Programming Language Functions Eng. Anis Nazer First Semester 2016-2017
Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities statements in a function belong to that function Up until now, all our commands are written inside "main()" function In C++, programs are made up of a collection of functions... like building blocks (... LEGO?)
Definitions main( )............ // function call............ function call return func ( )............... return
Definitions A function can be executed through a "function call" When a function call occurs, execution jumps to the "called function" execution then continues in the "called function" After the statements in the "called function" are finished, execution returns back to the "calling function"
Definitions What is the definition of the terms? function call? called function? calling function? return?
functions Predefined functions : functions are already written and ready for you to use thanks to the people who did that... :) ex. math functions: sqrt(), pow(), sin(), cos()... etc ex. rand()... remember the guessing game? User defined functions : you write your own function how fun is that... :)
Math functions There are millions of predefined functions available... a sea of functions Some are published and are available for you for free :) Predefined math functions: sqrt(), sin(), cos(), floor(), ceil(), pow(), log(),... To use the predefined math functions, you have to include the library <cmath> the file contains the definitions of the functions
Math functions Example: What is the output? #include <iostream> #include <cmath> using namespace std; int main() for ( int c=0 ; c <= 10 ; c++ ) cout << c << "\t" << pow(c, 2) << endl; return 0;
User defined functions To to write your function, you need to specify: function name number of parameters that the function receives and their data types (function input ) data type of the returned value (function output ) statements that are executed when the function is called
User defined functions Syntax: writing a user defined function return type function name ( formal ) parameter list statements......
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ;
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; function name if ( a > b ) max = a; else max = b; return max ;
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ; formal parameter list
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; formal parameters else max = b; return max ;
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; type of the if ( a > b ) returned value max = a; else max = b; return max ;
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ; function heading
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return max ; function body
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; local variable else max = b; return max ;
User defined functions A sample function that receives two numbers and returns the maximum value double larger ( double a, double b ) double max ; if ( a > b ) max = a; else max = b; return value return max ;
User defined functions function heading : the line that specifies: the return data type the function name the formal parameter list function body : the statements that are executed when the function is called local variable : the variable "belongs to the function, visible only to the function formal parameter list : the list of the parameters that the function expects formal parameters : variables declared in the function heading
User defined functions formal parameter list syntax: datatype identifier, datatype identifier,... Notes: list can have any number of parameters list can be empty, i.e. no parameters parentheses are part of the syntax, even if list is empty each parameter must have a data type it is a variable declaration
Example Write a program that reads two numbers and displays the larger number, use the previous function // function definition here //... int main () double x, y, m; cout << "Enter two numbers: \n"; cin >> x >> y; m = larger( x, y ) ; cout << "larger number is " << m; return 0; function call
Example larger( x, y ) function call What happens when a function is called? values of the actual parameters are copied into the formal parameters value of x is copied into variable a value of y is copied into variable b function is executed when finished, execution returns back to the calling function value of max is returned back to main() and replaces the function call
User defined functions Function call syntax: function name ( actual parameter list ) Syntax for actual parameter list value variable expression value, variable,... expression Note: number must match the number in the formal parameter list
functions - Types Value returning function: when the function ends, it returns a value to the "calling function" void functions: when the function ends, it returns to the "calling function" without returning a value
User defined function return statement ends the function and execution goes back to the calling function Syntax: Value returning function: return value/variable/expression ; void function: return ;
Example write a function that takes the length and width of a rectangle and returns the area Question: Input? two numbers: length, width Output? area types? double? how? area = length * width
Example double area ( double length, double width) double a; area = length * width; return area;
Another example Write a function that takes three numbers and returns the largest of the three double larger3 ( double n1, double n2, double n3) double m; m = larger ( n1, n2 ); m = larger ( m, n3 ); return m;
Another example Write a function that takes three numbers and returns the largest of the three or, you can reduce the code: double larger3 ( double n1, double n2, double n3) return larger ( larger ( n1, n2 ), n3 );
Example Write a program that reads 10 numbers and displays the maximum Use the already available function larger() Variables: number, max functions: larger how: read number max = larger of number and max repeat 10 times
Example read number Start max = number c=1 c <= 9 display max End read number increment c max = larger( max, number)
Example int main() double number, max; int c; cout << "Enter 10 numbers:\n"; cin >> number; for ( max = number, c=1; c <= 9 ; c++ ) cin >> number; max = larger( max, number ); cout << "The largest number is: "; cout << max; return 0;
More examples Write a function that takes a grade and returns true if the grade is between 0 and 100, and returns false otherwise Questions: Input? grade Output? true or false types? integer and boolean how? if/else
Example bool is_valid ( int grade ) if ( grade > 100 grade < 0 ) return false; return true;
Example Write a program that reads a grade and displays whether the grade is valid or not int main() int grade; cout << "Enter grade: "; cin >> grade; if ( is_valid ( grade ) ) cout << grade << " is valid"; else cout << grade << " is not valid"; return 0;
function prototype you can define the function after the function call, but you need to write a function prototype syntax: same as the function heading but with a semicolon at the end You can remove the variable name in the function prototype. In fact, the header files that are included (iostream, cmath,...) contain function prototypes
function prototype So, our example can be written as: // function prototype bool is_valid ( int grade ); int main() int grade; cout << "Enter grade: "; cin >> grade; if ( is_valid ( grade ) ) cout << grade << " is valid"; else cout << grade << " is not valid"; return 0; // function definition
Void functions void is a data type that refers to nothing Syntax: void function name ( formal ) parameter list statements......
Void functions void function returns to the calling function without returning a value the return statement in a void function has no value if the programmer does not write a return, the computer returns automatically, after executing the last statement in the function
Example write a function that takes an integer N and displays N stars on the screen write a function that takes an integer N and displays N spaces on the screen Write a program that displays a pyramid of stars, of height 10 Modify program to make the user enter the height
Stars void stars(int N) for ( int c=1; c <= N ; c++) cout << "*"; return ; void spaces(int N) for ( int c=1; c <= N ; c++) cout << " "; return ; int main() int sp, st, lines; cout << "Enter the number of lines: "; cin >> lines; for (int c=1, st=1, sp=lines; c <= lines ; c++) spaces(sp); stars(st); cout << endl; sp = sp 1; st = st + 2; return 0;