Chapter 5 Functions Contents Introduction Library functions Userdefined
Chapter 5: Functions
Contents • • Introduction Library functions User-defined functions Functions with parameter – Value – Reference
What is function • A mini-program containing algorithms to do a specific task • A function could be invoked (called) many times through its name (to execute its task), by the main function or other functions • When a function is invoked, the computer will execute all the instructions in the function from start to end, and then it goes to the instruction after the function call
Function classification • 2 types of functions : 1. Predefined function or built-in or library function 2. Independent function or user-defined function
Library Function • Functions that are ready-made by the producer of a compiler and stored in the header files (. h) called libraries • To use the predefined function, the appropriate library file must be included in the preprocessor directive (# include) • Common header files are : a) iostream. h b) iomanip. h c) string. h d) char. h e) math. h f) stdlib. h
Library Function
Library Function
Library Function call
Library Function
Library Function call
Library Function
User-defined Function • functions written by the programmer • the way the functions are used (called) is the same as library functions • to use a function, only the prototype and a description on what the function does is sufficient • 3 requirements to use the independent function : – Function prototype declaration – Function call – Function definition
User-defined Function 1 Click here 3 Click here 2 Click here
User-defined Function – Function Prototype • It is a function declaration. • Placed just before the main program and sometimes at the beginning of the main program, a function declaration specifies: – The return type – The name of the function – The order and type of parameters • The purpose of function prototype allows the compiler to check for the existence of the function, the parameter list and the return type is correct.
User-defined Function – Function Prototype • The syntax for the independent function prototype declaration is given below:
User-defined Function – Function Prototype • The syntax for the independent function prototype declaration is given below:
User-defined Function – Function Prototype • The following are examples of the function prototype:
User-defined Function – Function Prototype Exercise 1 Exercise 2: write function prototype for each of the following problems
User-defined Function – Function definition • A function must be defined before it can carry out the task assigned to it. • In other words, the statements that perform the task are written inside the function definition. • A function is written once in the program and can be used by any other function in the program. • The function definition is placed either before the main() or after the main().
User-defined Function – Function definition
User-defined Function – Function definition
User-defined Function – Function definition • A function definition consist of a function header and function body as shown below:
User-defined Function – Function definition return. Type function. Name (type parameter_list) { declaration(s); statement(s); return expression; }
User-defined Function – Function definition return. Type function. Name (type parameter_list) { declaration(s); statement(s); return expression; }
User-defined Function – Function definition • An example of function definition that accepts three values, calculates the total and display the total. void sum 3 Num(int a, int b, int c) { int total = a + b + c; cout<<total; } Explanation • The keyword void in the function header indicated the function that does not return a value after completing its assigned task (called void function) • The parameter-list int a, int b and int c indicated that three integer values will be sent into (accept/ receive) and used in the function sum 3 Num �
User-defined Function – Function definition • An example of function definition that accepts three values, calculates the total and return the total to the main function. int sum 3 Num(int a, int b, int c) { int total = a + b + c; return total; } Explanation • The keyword int in the function header indicated that the function will return an integer value. • The parameter-list int a, int b and int c indicated that three integer values will be sent into (accept/ receive) and used in the function sum 3 Num • return total indicated that the value of total will be returned to the calling function �
User-defined Function – Function definition Exercise 1
User-defined Function – Function definition Exercise 1
User-defined Function – Function definition Exercise 2
User-defined Function – Function definition Exercise 3
User-defined Function – Function call • A function is made to perform its designated task by a function call in main function or in other function. • When a function is called, the program control is passed to the called function (function definition) and the statements inside the function will be executed to the end until control is passed back the function call (function invocation). • To call a function, specify the function name and the values of the parameters that the function needs to do it job. • The following syntax is used when calling a function: Function_name(actual parameters);
User-defined Function – Function call • Example of function calls: void main() { find. Min(num 1, num 2); }
User-defined Function – Function call • Completing a function call – Function call that returns no value (void function) – Function call that returns a value
User-defined Function – Function call • Function call without value returned (void function) – This type of function does not return any value to the calling function (function call). – Upon completing the last statement in the function definition, the control is passed back to the statement that calls the function in the calling function (function call). – Thus, the next statement in the calling function will be executed.
User-defined Function – Function call Explanation • The statement calc. Sum(num 1, num 2, num 3); causes the function calc. Sum() to be executed. • Upon completing the execution in the calc. Sum(), control passed back to the calling statement in the main(). • The next statement, cout << “Next process…”; will get executed. Sample output:
User-defined Function – Function call • Function call with value returned – A function may also return a value. In this type of function, when the execution in the called function (function definition) is complete, the control is passed back to the calling function with a value. This value will be used in the calling function. – There are four ways how a returned value can be used in the calling function. • • In an arithmetic expression In a logical expression In an assignment statement In an output statement
User-defined Function – Function call
User-defined Function – Function call Exercise 1
User-defined Function – Function call Exercise 2
User-defined Function – Function call Exercise 3
User-defined Function – parameter • There are two types of parameters: – Formal parameter – Actual parameter
User-defined Function – parameter • Formal parameter – Formal parameter is arguments in the header of function definition. – Formal parameter contains the values that being passed by actual parameter. – The number of the formal parameters must agree with the number of actual parameters passed from the function call. – Example:
User-defined Function – parameter • Actual parameter – Actual parameter could be a constant, variable or expression in a function call that corresponds to the formal parameter. – Actual parameters contain values that will be passed to the function definition – Example:
User-defined Function • In summary, when using functions, we can have four types of functions. – Function without value returned and without parameter – Function without value returned but with parameter – Function with value returned but without parameter – Function with value returned and with parameter
User-defined Function • Function without value returned and without parameter
User-defined Function • Function without value returned but with parameter
User-defined Function • Function with value returned but without parameter
User-defined Function • Function with value returned and with parameter
User-defined Function- how values are pass between function? • A function communicates with another function by passing values between them. • A function can send data to other function, and it can also accept values sent by other functions. • Data can be passed between function through – Global variable Not advisable – Parameter passing – A return value
User-defined Function- how values are pass between function? • Parameter passing – Parameters can be passed from the calling function (function call) to the called function (function definition) using two type of passing which are: • Parameter passing by value • Parameter passing by reference
User-defined Function- how values are pass between function? • Parameter passing – pass by value – When passing by value, a copy of that value is passed from the calling function to the called function through the parameters. – The changes made to the copied value do not change the original value.
User-defined Function- how values are pass between function? Calling function Called function (function definition) main() num 4 Displayvalue() value 4 12
User-defined Function- how values are pass between function?
User-defined Function- how values are pass between function? • Parameter passing – pass by reference – When there is more than one value to be returned to the calling function or changed during the program execution, reference parameter is used. – When a parameter is sent by reference, the function actually gets the memory location of the actual parameter it can directly access the data. – In other words, the address of the variable in the computer’s memory is actually being passed. As a result, the changes made to the variable will affect the original value. – When passing by reference, the actual parameter in the calling function must be variable and the formal parameter must use the reference operator, which is referred by the symbol ampersand (&). The reference operator forces the corresponding actual formal parameters to refer to the same location. – In addition, the returned-type for passing parameters by reference is typically given the type void.
Independent Function- how values are pass between function?
Example - Pass by value #include <iostream. h> void add. Four(int a, int b) { Value parameter a = a + 4; b++; cout<<a<<b<<endl; } void add. Four(int a, int b); void main() { int num 1, num 2; num 1 = 3; num 2 = 1; cout<< num 1<<num 2<<endl; add. Four(num 1, num 2); cout<<num 1<<num 2<endl; } In main BEFORE calling add. Four function In main AFTER calling add. Four function num 1 3 a 7 num 1 3 num 2 1 b 2 num 2 1
Example - Pass by reference #include <iostream. h> void add. Four(int &a, int b) { a = a + 4; //line 1 b++; //line 2 cout<<a<<b<<endl; } Reference parameter When the value of a changes, value num 1 changes as well void add. Four(int& a, int b); void main() { int num 1, num 2; num 1 = 3; num 2 = 1; cout<< num 1<<num 2<<endl; add. Four(num 1, num 2); cout<<num 1<<num 2<endl; } In main BEFORE calling add. Four function In main AFTER calling add. Four function num 1 3 a 7 num 1 7 num 2 1 b 2 num 2 1
Example - Pass by reference • How it works? In main BEFORE calling add. Four function In add. Four function num 1 3 a num 2 1 b 1 Before statement in line 1 execute!! In main BEFORE calling add. Four function In add. Four function num 1 7 a num 2 1 b 2 In function add. Four, after statement in line 1 and line 2 execute!!
User-defined Function- how values are pass between function?
Independent Function- how values are pass between function?
Summary • Void function: a function that does not have a data type • A return statement without any value can be used in a void function to exit function early • The heading of a void function starts with the word void • To call a void function, you use the function name together with the actual parameters in a stand-alone statement
Summary • Two types of formal parameters: value parameters and reference parameters • A value parameter receives a copy of its corresponding actual parameter • A reference parameter receives the address (memory location) of its corresponding actual parameter
Summary • If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter • Variables declared within a function (or block) are called local variables • Variables declared outside of every function definition (and block) are called global variables
The end…
- Slides: 64