You are on page 1of 41

BITG 1113:

Function
(Part 1)

LECTURE 6

1
Objectives

• Prototype and function declaration


• The function call and returning value
• Local and global variables

2
Types of Function
• Predefined Function
• User-defined Function

3
Predefined 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 program
exp : #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
User-Defined 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
Declaring, calling and defining functions

6
Function Definition
• 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
Programming Example
Define a function Invoke a funciton

return value type method name formal parameters

function int z = max(x, y);


int max(int num1, int num2)
header
{

function actual parameters


int result; parameter list (arguments)
body
if (num1 > num2)
result = num1; return value
else
result = num2;

return result;
}

18
pass the value i
pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

19
Trace Function Invocation
i is now 5

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

20
Trace Function Invocation
j is now 2

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

21
Trace Function Invocation
invoke max(i, j)

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

22
Trace Function Invocation
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 max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

23
Trace Function Invocation
declare variable result

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

24
Trace Function Invocation
(num1 > num2) is true since
num1 is 5 and num2 is 2

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

25
Trace Function Invocation
result is now 5

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

26
Trace Function Invocation
return result, which is 5

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

27
Trace Function Invocation
return max(i, j) and assign the
return value to k

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; return result;
} }

28
Trace Function Invocation
Execute the print statement

pass the value i


pass the value j

int main() int max(int num1, int num2)


{ {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
cout << "The maximum between " else
<< i << " and " + j + " is " result = num2;
<< k;
return 0; 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
Local Variables
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
Local Variables
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.

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

33
Local Variables

It is fine to declare i in two It is illegal to


non-nesting blocks declare i in two nesting blocks

void function1() void function2()


{ {
int x = 1; int i = 1;
int y = 1; int sum = 0;

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

for (int i = 1; i < 10; i++) cout << i << endl;


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

34
Global Variables
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.

35
#include <iostream>
using namespace std;
  Example of Variable Scope
int y;
void t1();
void t2();
int main()
{
t1();
t2();
return 0;
}
 
void t1()
{
int x = 1;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
x++;
y++;
}

void t2()
{
int x = 1;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}

36
Unary Scope Resolution
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 <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;
}
37
Static Local Variables
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.

38
When ‘static’ is used
 
#include <iostream>
using namespace std;
 
void t1();
 
int main()
{
t1();
t1();
return 0;
}
 
void t1()
{
static int x = 1;
int y = 1;
x++;
y++;
cout << "x = " << x << endl;
cout << "y = " << y << endl << endl;
}

39
When ‘static’ is NOT used
 
#include <iostream>
using namespace std;
 
void t1();
 
int main()
{
t1();
t1();
return 0;
}
 
void t1()
{
int x = 1;
int y = 1;
x++;
y++;
cout << "x = " << x << endl;
cout << "y = " << y << endl << endl;
}

40
THANK YOU

41

You might also like