You are on page 1of 33

1

Chapter 2:
C++ FUNDAMENTALS
(Part 3)
DCS5088 :: Chapter 2 (part 3) 2
Objectives
At the end of this lecture, students should
be able to :
Create functions and implement them
call by value
call by reference (pointers)
call by reference (reference arguments)
Understand variables scope
DCS5088 :: Chapter 2 (part 3) 3
2.15 Functions
Modules
The C++ library - a rich collection of functions and classes
Programmer defined functions, classes
A function invoked by a function call.
The function call specifies the function name and
provides information that the call function needs to do
its job.
Functions allow the programmer to modularize a
program.
DCS5088 :: Chapter 2 (part 3) 4
2.15.1 Analogy
Boss to worker analogy
A boss (the calling function or caller) asks a
worker (the called function) to perform a task
and return (i.e., report back) the results
when the task is done.

DCS5088 :: Chapter 2 (part 3) 5
2.16 Math Library Functions
Perform common mathematical calculations
Include the header file <cmath>
Functions called by writing
functionName (argument);
or
functionName(argument1, argument2, );
Example
cout << sqrt( 900.0 );
sqrt (square root) function The preceding statement would
print 30
All functions in math library return a double
DCS5088 :: Chapter 2 (part 3) 6
2.17 Functions: 3 things to include in a
program
Function definition / called function
Function prototype
Function call / invocation or calling
function
DCS5088 :: Chapter 2 (part 3) 7
#include<iostream>
using namespace std;
// function prototypes here
.

void main()
{ .
//function call
..

}

//function definition
DCS5088 :: Chapter 2 (part 3) 8
2.17.1 Function Definition
Below is the format:
Return-value-type function-name (parameter-list)
{
declarations and statements
}

Parameter list may be empty or have 1 or
more variables
Variables in the parameter list have a local scope.

DCS5088 :: Chapter 2 (part 3) 9
Function Def. Example 1
Create a function that accepts 2 integers and return
the total as an integer value

int func1(int n1, int n2)
{ int total;
total = n1 + n2;
return total;
}
How many
local
variables are
there?
DCS5088 :: Chapter 2 (part 3) 10
Function Def. Example 2
Create a function that accepts 2 integers and
displays the total result

void func2(int n1, int n2)
{ int total;
total = n1 + n2;
cout<<total is <<total<<endl;
}
DCS5088 :: Chapter 2 (part 3) 11
Function Def. Example 3
Create a function that gets an integer number from
user and returns the number

int func3( )
{ int num;
cin>>num;
return num;
}

DCS5088 :: Chapter 2 (part 3) 12
Function Def. Example 4
Create a function that gets an integer number from
user and displays the square of the number

void func4( )
{ int num, result;
cin>>num;
result = num * num;
cout<<result<<endl;
}

DCS5088 :: Chapter 2 (part 3) 13
2.17.2 Function prototypes
A function prototype tells the compiler the
the name of a function,
the type of the data returned,
the number of parameters,
the type of those parameters,
the order in which the parameter of those types
are expected.
The compiler use function prototype to validate
function calls.
DCS5088 :: Chapter 2 (part 3) 14
Function Prototype. Example 1
Given the function definition:
int func1(int n1, int n2)
{ int total;
total = n1 + n2;
return total;
}
So, the function prototype:
int func1(int, int);
DCS5088 :: Chapter 2 (part 3) 15
Function Prototype. Example 2
Given the function definition:
int func3( )
{ int num;
cin>>num;
return num;
}
So, the function prototype:
int func3();

DCS5088 :: Chapter 2 (part 3) 16
2.17.3 Function call
Function that returns a value to the caller.
need to assign the value returned by the function to a
variable on the left
Example:
val = func1(a, b);
n1 = calculate (100, 2);
Function that does not return any value to the caller
simple
Example:
func4( );
display(199.59);
DCS5088 :: Chapter 2 (part 3) 17
2.18 Methods of Data accessibility for
functions
Call by value
Call by reference using pointers
Call by reference using reference
parameters
Global variables
DCS5088 :: Chapter 2 (part 3) 18
Call by Value
When the data is passed to a function,
the value of the variable is copied into the
input variable parameters.
Called function has own copies of data
passed from calling function
DCS5088 :: Chapter 2 (part 3) 19
Full program Example 1
Create a function that accepts 2
arguments from the main() and returns
the total to main()
In the main, get user inputs for 2 integers
and call the function to return the total.
Display the total

DCS5088 :: Chapter 2 (part 3)
20
#include<iostream>
using namespace std;
int calc(int, int);

int main()
{ int n1,n2, total;
cout<<"Enter 2 numbers :";
cin>>n1>>n2;

total = calc(n1, n2);

cout<<"The total of "<<n1<<" + "<<n2<<
" is "<<total<<endl;
return 0;

}

int calc(int a, int b)
{ return a + b;
}
Function Call
Called
Function /
Function
Definition
Function
Prototype
DCS5088 :: Chapter 2 (part 3) 21
Output:
DCS5088 :: Chapter 2 (part 3) 22
Full program Example 2
Create a function that accepts 2
arguments from the main() and displays
the total result
In the main, get user inputs for 2 integers
and call the function to show the total.
DCS5088 :: Chapter 2 (part 3)
23
#include<iostream>
using namespace std;
void calc(int, int);

int main()
{ int n1,n2;
cout<<"Enter 2 numbers :";
cin>>n1>>n2;

calc(n1, n2);

return 0;
}

void calc(int a, int b)
{
int total;
total = a + b;
cout<<"The total of "<<a<<" + "<<b<<
" is "<<total<<endl;
}
Function Call
Called
Function /
Function
Definition
Function
Prototype
DCS5088 :: Chapter 2 (part 3) 24
Output:
Still same as example 1 but different
implementation
DCS5088 :: Chapter 2 (part 3) 25
2.18 Methods of Data accessibility for
functions
Call by value
Call by reference using pointers
Call by reference using reference
parameters
Global variables
DCS5088 :: Chapter 2 (part 3) 26
Call by Reference (using pointers)
The address of the data variable is passed
from the calling function(from main) to a called
function. The address is copied into a pointer
variable that belongs to the called function.
Called function does not have its own local
copy of the variable.
The indirection operator(*) is used with the
pointer to access the calling functions data.
DCS5088 :: Chapter 2 (part 3) 27
Example 1
Create a function that accepts 3 arguments
from the main() through pointers. The first two
pointers refers to the integers from main and
the third pointer refers to the result variable.
The function will set the result variable to the
addition of the 2 integers at main().
In the main, get user inputs for 2 integers and
call the function. After that show the total.
DCS5088 :: Chapter 2 (part 3)
28
#include<iostream>
using namespace std;
void calc(int*, int*, int*);

int main()
{ int n1, n2, total;
cout<<"Enter 2 numbers :";
cin>>n1>>n2;

calc(&n1,&n2, &total);
cout<<"The total of "<<n1<<" + "<<n2<<" is
"<<total<<endl;
return 0;
}

void calc(int *p1, int *p2, int *tot)
{ *tot = *p1 + *p2;

}
DCS5088 :: Chapter 2 (part 3) 29
Call by Reference (using
reference arguments)
Another technique for passing the variables address to
a function
A reference parameter is an address. Reference
parameters employ the reference operator (&) in the
function declaration. The reference variables in called
function are implicit pointers
The variable name is used in the call statement(from
main) but actually the address of the variable is passed
to the called functions parameters.
DCS5088 :: Chapter 2 (part 3) 30
Example 1
Create a function that accepts 3 arguments
from the main() through reference arguments.
The first two reference argument refers to the
integers from main and the third reference
refers to the result variable. The function will
set the result variable to the addition of the 2
integers at main().
In the main, get user inputs for 2 integers and
call the function. After that show the total.
DCS5088 :: Chapter 2 (part 3)
31
#include<iostream>
using namespace std;
void calc(int&, int&, int&);

int main()
{ int n1, n2, total;
cout<<"Enter 2 numbers :";
cin>>n1>>n2;

calc(n1,n2,total);
cout<<"The total of "<<n1<<" + "<<n2<<" is
"<<total<<endl;
return 0;
}

void calc(int &p1, int &p2, int &tot)
{ tot = p1 + p2;

}
Calling function (call by
value) but it is actually call by
reference using reference
arguments as you can see in
the function parameters.
DCS5088 :: Chapter 2 (part 3) 32
2.19 Variable Scope
WHERE you declare a variable
Local
A local variable is a variable that is declared
within a function, or within any block of code.
Global
A variable declared outside of any function
can be used in any function

DCS5088 :: Chapter 2 (part 3) 33
2.20 Static Variable
A local variable that retains its value in a
function that can be called a number of
times till the entire program terminates.
They are still known only in the function in
which they are declared,
Retain their values when the function
returns to its caller.

You might also like