function

38
1

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.

Transcript of function

Page 1: function

1

Page 2: function

Prototype and function declaration The function call and returning value Local and global variables

2

Page 3: function

Predefined Function User-defined Function

3

Page 4: function

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

Page 5: function

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

Page 6: function

6

Page 7: function

Contains the code for a function. Two parts : the function header and the function

body

7

Page 8: function

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.

Page 9: function

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.

Page 10: function

10

Function local variables

Page 11: function

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

Page 12: function

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.

Page 13: function

13

More examples of function calls

Page 14: function

14

Parts of a function call

The Function Call

Page 15: function

15

Calling a void function with no parameters

Void functions with no parameters

Page 16: function

16

Void functions with parameters

Page 17: function

17

Pass by Value

Functions that return value

Page 18: function

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)

Page 19: function

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

Page 20: function

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

Page 21: function

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

Page 22: function

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)

Page 23: function

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

Page 24: function

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

Page 25: function

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

Page 26: function

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

Page 27: function

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

Page 28: function

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

Page 29: function

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

Page 30: function

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)

Page 31: function

31

Scope for global and block areas

Page 32: function

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

Page 33: function

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

Page 34: function

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

Page 35: function

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

Page 36: function

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;

}

Page 37: function

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

Page 38: function

38