1
Prototype and function declaration The function call and returning value Local and global variables
2
Predefined Function User-defined Function
3
Its programming code is already written. A programmer only need to know how to use it. Need to include the header file in the program exp : #include Some of the predefined mathematical functions in header file cmath are: The power function, pow(x,y) The square root function, sqrt(x) The floor function, floor(x)
4
The name of function is used in three ways : for declaration, in a call, and for definition.
Function declaration is done first with a prototype declaration.
Function definition contains the code to complete the task.
Function is invoked or called by Function call.
5
6
Contains the code for a function. Two parts : the function header and the function body
7
Function Header Consist of : the return type, the function name and formal parameter list Return type The type of value that will return by the function, the type of the expression in the return statement must match the return type in the function header. For example void, int, char and double. •Formal Parameter List List that defines and declares the variables that will contained the data received by the function Each variables must be defined and declared fully with multiple parameters separated by commas.
8
Function Body • Contains the declarations and statements for the function • Start with local definitions that specify the variables required by the function. • The functions statement, terminating with a return statement are coded after local definitions.
9
Function local variables 10
Prototype declaration • Consist of three parts : the return part, function name, and the formal parameter list(has to be same as in function header). • Terminated with semicolon. • Placed in global area of the program • General format: Type Function_name(parameter_list);
• Example : double average (int x, int y); double average (int, int); void display ( ); char pilihan ( );
11
The Function Call • The operand in a function call is the function name. • The operator is the parentheses set,(…), which contains the actual parameters. Formal parameters are variables that are declared in the header of the function definition. Actual parameters are the expressions in the calling statement. The formal and actual parameters must match exactly in type, order and number. Their names however, do not need to be the same.
• Examples of the function calls : cout << average ( 3, 7); avg = average ( z, x); cout << average ( 3, 7) + 5;
12
More examples of function calls 13
The Function Call
Parts of a function call 14
Void functions with no parameters
Calling a void function with no parameters 15
Void functions with parameters
16
Functions that return value
Pass by Value 17
Invoke a funciton
Define a function return value type function header
method name
formal parameters
int max(int num1, int num2) {
function body
int result; if (num1 > num2) result = num1; else result = num2; }
parameter list
int z = max(x, y); actual parameters (arguments)
return value
return result;
18
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
19
i is now 5
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
20
j is now 2
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
21
invoke max(i, j)
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
22
invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
23
declare variable result
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
24
(num1 > num2) is true since num1 is 5 and num2 is 2
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
25
result is now 5
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
26
return result, which is 5
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
27
return max(i, j) and assign the return value to k
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
28
Execute the print statement
pass the value i pass the value j int main() { int i = 5; int j = 2; int k = max(i, j);
}
cout << "The maximum between " << i << " and " + j + " is " << k; return 0;
int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; }
return result;
29
Variable Scope Scope-determines the the part of program in which you can use defined object. • Global scope – any object defined in the global area of the program is visible from its definition until the end of the program. -global variables : variables that are declared outside the function, recognised by any function or program that start after its declaration
• Local scope – variable defined within a block, visible only in the block in which they are declares. - local variables :variables that are declared in the function body and can only be used in that particular function. - do not relate to any variable in other function (can have the same name as variables in other functions) 30
Scope for global and block areas 31
You can declare a local variable with the same name multiple times in different non-nesting blocks in a function, but you cannot declare a local variable twice in nested blocks.
32
A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.
The scope of i The scope of j
void method1() { . . for (int i = 1; i < 10; i++) { . int j; . . . } }
33
It is fine to declare i in two non-nesting blocks void function1() { int x = 1; int y = 1;
It is illegal to declare i in two nesting blocks void function2() { int i = 1; int sum = 0;
for (int i = 1; i < 10; i++) { x += i; }
for (int i = 1; i < 10; i++) { sum += i; }
for (int i = 1; i < 10; i++) { y += i; }
cout << i << endl; cout << sum << endl;
}
}
34
C++ also allows you to use global variables. They are declared outside all functions and are accessible to all functions in its scope. Local variables do not have default values, but global variables are defaulted to zero.
VariableScope Demo
35
If a local variable name is the same as a global variable name, you can access the global variable using ::globalVariable. The :: operator is known as the unary scope resolution. For example, the following code: #include using namespace std; int v1 = 10; int main() { int v1 = 5; cout << "local variable v1 is " << v1 << endl; cout << "global variable v1 is " << ::v1 << endl; return 0; } 36
After a function completes its execution, all its local variables are destroyed. Sometimes, it is desirable to retain the value stored in local variables so that they can be used in the next call. C++ allows you to declare static local variables. Static local variables are permanently allocated in the memory for the lifetime of the program. To declare a static variable, use the keyword static.
StaticVariable Demo
37
38