PROGRAMMING
FUNDAMENTALS
Dr. Sheikh Faisal Rashid
shfaisal@gmail.com,uet.edu.pk
Department of Computer Science and
Engineering UET Lahore
FUNCTIONS
Outline
• In this lesson, we will:
• Review mathematical functions
• Look at the use of functions in C++
• Describe function declarations
• Their relation to the domain and range
• Parameters and arguments
• Example: a fast sine function
• Side effects
• Functions with no return values
• The void keyword
• A second-last look at understanding int main()
Using functions
• In secondary school mathematics courses, you were
introduced to numerous functions:
sin x , cos x , etc.
• The trigonometric functions
• Possibly including hyperbolic functions and the xinverses of these
e , ln x , log 10 x
• The exponential and logarithmic functions
x
• The absolute value
x
• The square root
x , x
• The ceiling and floor functions
• The greatest common divisor and least common multiple functions
• The maximum or minimum of two arguments
gcd m , n , lcm m , n
max m , n , min m , n
Using functions
• All of these have some properties in common:
• Each function requires a fixed number of arguments that must be of
a certain type, either integers or real numbers
• Given the same arguments, the functions return the same value
• Many of these functions are implemented in the cmath
library
Output:
sin(3.2) = -0.0583741
Using functions tan(3.2) = 0.0584739
csc(3.2) = -17.1309
cot(3.2) = 17.1017
sinh(3.2) = 12.2459
#include <iostream> tanh(3.2) = 0.996682
#include <cmath> acos(0.2) = 1.36944
Must use std::sin( 3.2 ) asech(0.2) = 2.29243
int main();
– Mathematicians sometimes use sinq or sin2q
int main() {
std::cout << "sin(3.2) = " << std::sin( 3.2 ) << std::endl;
std::cout << "tan(3.2) = " << std::tan( 3.2 ) << std::endl;
std::cout << "csc(3.2) = " << (1.0/std::sin( 3.2 )) << std::endl;
std::cout << "cot(3.2) = " << (1.0/std::tan( 3.2 )) << std::endl;
std::cout << "sinh(3.2) = " << std::sinh( 3.2 ) << std::endl;
std::cout << "tanh(3.2) = " << std::tanh( 3.2 ) << std::endl;
std::cout << "acos(0.2) = " << std::acos( 0.2 ) << std::endl;
std::cout << "asech(0.2) = " << std::acosh( 1.0/0.2 ) << std::endl;
return 0;
}
Using functions
• In each case, the argument is of type double
• A double-precision floating-point number
• The output is a floating-point number
• The compiler knows this, so while you compile
std::cout << std::sin(32);
the compiler knows that:
• The integer 32 must be converted to a floating-point number
• The above statement must call the routines for printing a floating-point
number
Function declarations
• How does the compiler know this about the sine function?
• We must declare the sine function in a manner similar to main()
int main();
• This says main() does not have any parameters and it
returns an integer
• For the sine function, we know it has a domain and range:
sin : R R
name domain range
Function declarations
• Suppose we wanted to define a polynomial p such that
when it is called with an argument x, it returns the value
3x2 4 x 2
• To this point, we have seen that int represents that the
return type is an integer
• Polynomials, however, are defined for all real numbers
• Floating point numbers in C++
• A type for floating-point numbers is double
• Short for double-precision floating-point numbers
Function parameters
• Additionally, the polynomial
3x2 4 x 2
requires a variable or parameter x
• x can take on any real value, so we call it a variable
• The result of the polynomial depends on x, so we say x is a parameter
• The function declaration is
double p( double x );
• This function:
• Has the identifier p
• Takes a variable parameter x that must be a floating-point number
• Returns a floating-point number
Function definitions
• The function int main() simply returned 0
• To evaluate def
p x 3x2 4 x 2
we must perform an arithmetic calculation
• The function definition would be:
double p( double x ) {
return 3*x*x + 4*x - 2;
}
Parameters and arguments
• In the function definition, variable x is referred to as a
parameter:
double p( double x ) {
return 3*x*x + 4*x - 2;
}
the parameter x
• If you call this function with a value that is to be used in the
function, that value is said to be the argument:
the argument 4.2
int main() {
std::cout << "p(4.2) = " << p( 4.2 )
<< std::endl;
}
Another example
• Suppose we wanted to define a bivariate polynomial q
such that when it is called with arguments x and y, it
returns the value
x 2 2 xy y 2 x y x y
• The function declaration and definitions could be
double q( double x, double y );
double q( double x, double y ) {
return x*x - 2*x*y + y*y;
} Alternate function definition:
double q( double x, double y ) {
return (x - y)*(x - y);
}
Another example
• Question: which implementation is faster?
double q( double x, double y ) { double q( double x, double y ) {
return x*x - 2*x*y + y*y; return (x - y)*(x - y);
} }
• These require:
• Five multiplications and two addition/subtractions
• One multiplication and two addition/subtractions (or just one?)
The greatest common divisor
• The greatest common divisor (gcd) is another function you saw
in secondary school
• E.g., gcd(42, 70) = 14
• It depends on two integer parameters and returns an integer
gcd : Z Z N
name domain range
Cartesian product of two sets of integers natural numbers
• Its function declaration would be
unsigned int gcd( int m, int n );
Other examples
• Some functions you saw in secondary school were
represented graphically:
• Exponents were written as superscripts: xy
• The square root was written with a radical symbol: x
n
• nth roots had even further decorations: x
x
• The absolute value was two bars:
• In C++, we are restricted to functions and identifiers:
double pow( double x, double y );
double sqrt( double x );
double sqrt( double x, int n );
double abs( double x );
Other examples
• The standard mathematics library has some of these:
double std::pow( double x, double y );
double std::sqrt( double x );
double std::abs( double x );
• For the nth root, the user is expected to use pow:
1
n
x xn
Other functions
• Most functions require more than simple calculations
• Most require decision making processes:
x x0 x x y x x y
x max x , y min x , y
x x0 y x y y x y
• Others require a repetitive algorithm until some condition is met
• E.g., finding the gcd, calculating the square root
• In some cases, some functions can be defined in terms of others
• E.g., the least common multiple:
mn
lcm m , n
gcd m , n
Side effects
• In mathematics, the result of a function depends entirely
on the arguments
• Anything else a function does is called a side-effect
• The side-effect of the int main() function is to print “Hello world!”
to the console output
• A side effect of this function is to record to a log file what
was being calculated
double fast_sin( double x );
double fast_sin( double x ) {
std::clog << "Calculating p(" << x << ")" << std::endl;
return (-0.1290061377327980*x*x + 0.9549296585513720)*x;
}
No return value
• Some functions have no return value:
• Such functions are identified by describing the return type as void:
void print_my_name();
No return value
void print_my_name() {
std::cout << "Zaphod Beeblebrox" << std::endl;
return;
} Just return, don’t return any value
– You can even leave this off
Why not void main() ?
• The function declaration for main() has it returning an
int
• Executing programs can cause other programs to execute
• When a program exits, the value returned by main() could be used by
the program that launched it
• The value 0 is generally used to indicate “a successful execution”
• If something went wrong, the program could return a non-zero
integer that can be used to flag what the issue was
• For this course, main() will always return 0
Arithmetic expression
• Prior to this, we described an arithmetic expression as:
1. A numeric value (integer or floating point):
…, -3, -2, -1, 0, 1, 2, 3, …
2. The chaining of two or more arithmetic expressions with
addition or subtraction:
(arith-1) + (arith-2) + (arith-3)
(arith-1) * (arith-2) * (arith-3)
3. The difference or ratio of two arithmetic expressions:
(arith-1) - (arith-2)
(arith-1) / (arith-2)
4. The negation of or applying the unary + operator to an
arithmetic expression:
-(logical-expression)
+(logical-expression)
Arithmetic expression
• We can now add two more valid representations of an
arithmetic expression:
5. A call to a function that returns an integer or floating-point type:
calling double function_name(…);
6. A variable (e.g., a parameter) that is declared to be of integer or
floating-point type:
double sin( double x );
• For example, this function returns a valid arithmetic
expression:
double f( double x, double y ) {
return -(3.0 + y)*(1.0 + 2.0*(std::sin( x ) - y));
}
Summary
• Following this lesson, you now:
• Understand that functions in C++ are called like functions in math
• Understand the purpose of the function declaration:
• The type of any parameters and the type of the return value
• Know the difference between parameters and arguments
• Know how to implement a simple function
• Can describe the difference between the return value and side
effects
• Know how to indicate a function has no return value: void
• Understand the int main() function
References
[1] cplusplus.com
http://www.cplusplus.com/reference/cmath/