C++ Function Part 1
C++ Function Part 1
Function
(Part 1)
LECTURE 6
1
Objectives
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.
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 );
14
Void functions with no parameters
16
Functions that return value
Pass by Value
17
Programming Example
Define a function Invoke a funciton
return result;
}
18
pass the value i
pass the value j
19
Trace Function Invocation
i is now 5
20
Trace Function Invocation
j is now 2
21
Trace Function Invocation
invoke max(i, j)
22
Trace Function Invocation
invoke max(i, j)
Pass the value of i to num1
Pass the value of j to num2
23
Trace Function Invocation
declare variable result
24
Trace Function Invocation
(num1 > num2) is true since
num1 is 5 and num2 is 2
25
Trace Function Invocation
result is now 5
26
Trace Function Invocation
return result, which is 5
27
Trace Function Invocation
return max(i, j) and assign the
return value to k
28
Trace Function Invocation
Execute the print statement
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
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
for (int i = 1; i < 10; i++) for (int i = 1; i < 10; i++)
{ {
x += i; sum += 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