function

Preview:

DESCRIPTION

this chapter is about the function part of c++ programming. this is just part 1, i will upload part 2 later. if you like it, please subscibd me. thank you.

Citation preview

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

programexp : #include<cmath>

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

8

Function HeaderConsist 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.

9

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.

10

Function local variables

11

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 ( );

12

The Function Call• The operand in a function call is the function name.

• The operator is the parentheses set,(…), which contains the actual parameters.

• Examples of the function calls :

cout << average ( 3, 7);

avg = average ( z, x);

cout << average ( 3, 7) + 5;

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.

13

More examples of function calls

14

Parts of a function call

The Function Call

15

Calling a void function with no parameters

Void functions with no parameters

16

Void functions with parameters

17

Pass by Value

Functions that return value

18

int max(int num1, int num2) {

int result; if (num1 > num2) result = num1; else result = num2; return result;

}

return value type method name formal parameters

return value

function body

function header

parameter list

Define a function Invoke a funciton

int z = max(x, y);

actual parameters (arguments)

19

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; }

pass the value i

pass the value j

20

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; }

pass the value i

pass the value j

i is now 5

21

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; }

pass the value i

pass the value j

j is now 2

22

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; }

pass the value i

pass the value j

invoke max(i, j)

23

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; }

pass the value i

pass the value j

invoke max(i, j)Pass the value of i to num1Pass the value of j to num2

24

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; }

pass the value i

pass the value j

declare variable result

25

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; }

pass the value i

pass the value j

(num1 > num2) is true since num1 is 5 and num2 is 2

26

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; }

pass the value i

pass the value j

result is now 5

27

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; }

pass the value i

pass the value j

return result, which is 5

28

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; }

pass the value i

pass the value j

return max(i, j) and assign the return value to k

29

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; }

pass the value i

pass the value j

Execute the print statement

30

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)

31

Scope for global and block areas

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.

33

void method1() { . . for (int i = 1; i < 10; i++) { . int j; . . . } }

The scope of j

The scope of i

34

void function1() { int x = 1; int y = 1;

for (int i = 1; i < 10; i++) {

x += i; }

for (int i = 1; i < 10; i++) {

y += i; } }

It is fine to declare i in two non-nesting blocks

void function2() { int i = 1; int sum = 0;

for (int i = 1; i < 10; i++) {

sum += i; } cout << i << endl; cout << sum << endl;

}

It is illegal to declare i in two nesting blocks

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:

36

#include <iostream>

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;

}

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

Recommended