Computer Programming
Department of Computer Science
Target Group : Computer Science Dept (1st year)
By: Bayisa G.
09/22/2025 .1
CHAPTER FOUR
Functions
What is a function?
Functions are building blocks of the programs.
They make the programs more modular and easy to read and
manage.
All C++ programs must contain the function main( ).
The execution of the program starts from the function main( ).
Modular programming: breaking a program up into smaller,
manageable functions or modules.
Supports the divide-and-conquer approach to solving a problem.
Function: A function is a group of statements that is executed when it
is called from some point of the program to perform a specific task.
• A function provides a convenient way of packaging a computational recipe,
09/22/2025so that it can be used as often as required. 2
• Therefore, a function is a block of code designed to tackle a specific
Computer Science problem.
Dept (Ambo University) 2
Advantages
Support for modular programming
Reduction in program size.
Code duplication is avoided.
Code reusability is provided.
Functions can be called repetitively.
A set of functions can be used to form libraries.
09/22/2025 3
Computer Science Dept (Ambo University) 3
Summarized function basics
C++ functions generally adhere to the following rules.
Every function must have a name.
Function names are made up and assigned by the programmer
following the same rules that apply to naming variables. They can
contain up to 32 characters, they must begin with a letter, and
they can consist of letters, numbers, and the underscore (_)
character.
All function names have one set of parenthesis immediately
following them. This helps you (and C++ compiler) differentiate
them from variables.
09/22/2025 4
Computer Science Dept (Ambo University) 4
Function Prototype
The prototype (also called interface of a function) above yields the
following information to the compiler:
func is the function name
the function is called with two arguments: the first argument is of
type int, the second of type double
the return value of the function is of type long.
09/22/2025 5
Computer Science Dept (Ambo University) 5
Function Prototype
A function has a name and a type, much like a variable.
Function prototypes are also known as function declarations.
Function prototypes are usually placed near the top of a program so
the compiler will encounter them before any function calls.
The function’s type is defined by its return value, that is, the value the
function passes back to the program.
In addition, the type of arguments required by a function is important.
Examples:
• int toupper(int c);
• double pow(double base, double exponent);
• NOTE: You must either place the function definition or the function
prototype ahead of all calls to the function. Otherwise the program will
not compile.
09/22/2025 6
Computer Science Dept (Ambo University) 6
Function Definition
A function definition is a declaration of a function that tells the
program about the type of value returned by the function, name of
function, number and type of arguments and body of the function.
type name ( parameter1, parameter2, ...) { statements }
• type is the data type specifier of the data returned by the function.
• name is the identifier by which it will be possible to call the function.
• parameters (as many as needed) and statements is the function's body.
Two types of function:
Built in functions :-
– are part of compiler package.
– Part of standard library made available by compiler.
– Can be used in any program by including respective header file.
User defined functions:-
– Created by user or programmer.
– Created as per requirement of the program.
09/22/2025 7
Computer Science Dept (Ambo University) 7
Function Header
Return Type
Name
Parameters list (this one is empty)
Body
The function header consists of
– the function return type
– the function name
– the function parameter list
Example:
int main()
Note: no ; at the end of the header
09/22/2025 8
Computer Science Dept (Ambo University) 8
Function Calling
A function must be called by its name followed by argument list
enclosed in semicolon.
To call a function, use the function name followed by () and ;
printHeading();
When a function is called, the program executes the body of the
function
When a function is called, the program may send values into the
function.
Values that are sent into a function are called arguments.
A parameter is a special variable that holds a value being passed as
an argument into a function.
The sequence of the arguments in the call of the function should be
same as the sequence of the parameters in the parameter list of the
declaration of the function.
After the function terminates, execution resumes in the calling module
09/22/2025 9
at the point of call Computer Science Dept (Ambo University) 9
Example
Output:
I am passing 5 to displayValue.
The value is 5
09/22/2025 Now I am back in main. 10
Computer Science Dept (Ambo University) 10
Example
Output: The result is 8
The parameters and arguments
have a clear correspondence.
Within the main function we
called to addition passing two
values: 5 and 3, that
correspond to the int a and int
09/22/2025
b parameters declared for 11
function addition.
Computer Science Dept (Ambo University) 11
Function Calling...
At the point at which the function is called from within main, the
control is lost by main and passed to function addition.
The value of both arguments passed in the call (5 and 3) are copied to
the local variables int a and int b within the function.
Function addition declares another local variable (int r), and by means
of the expression r = a + b, it assigns to r the result of a plus b.
Because the actual parameters passed for a and b are 5 and 3
respectively, the result is 8.
The following line of code: return (r); finalizes function addition, and
returns the control back to the function that called it in the first place
(in this case, main).
At this moment the program follows it regular course from the same
point at which it was interrupted by the call to addition.
09/22/2025 12
Computer Science Dept (Ambo University) 12
Output:
The first result is 5
The second result is 5
The third result is 2
09/22/2025 The fourth result is 6 13
Computer Science Dept (Ambo University) 13
Void Functions
It isn’t necessary for all functions to return a value, however.
Some functions simply perform one or more statements and then
terminate
These are called void functions. Example:
void displayMessage()
{
cout << "Hello from the function displayMessage.\n";
}
The function’s name is displayMessage. This name is descriptive, as
function names should be.
It gives an indication of what the function does: It displays a message.
Notice the function’s return type is void.
This means the function does not return a value to the part of the
program that executed it.
Also notice the function has no return statement. It simply displays a
09/22/2025 14
message on the screen and exits. Computer Science Dept (Ambo University) 14
Void Functions
Output:
I am starting in function main.
I am now inside the function deep.
I am now inside the function
deeper.
Now I am back in deep.
Back in function main again.
09/22/2025 15
Computer Science Dept (Ambo University) 15
Passing Arguments by Value
When an argument is passed into a parameter by value, only a copy
of the argument’s value is passed.
This means that when calling a function with parameters, what we
have passed to the function were copies of their values but never the
variables themselves.
For example, suppose that we called our first function addition using
the following code:
int x = 5, y = 3, z;
z = addition ( x , y );
What we did in this case was to call to function addition passing the
values of x and y, i.e. 5 and 3 respectively, but not the variables x and
y themselves.
This way, when the function addition is called, the value of its local
variables a and b become 5 and 3 respectively
but any modification to either a or b within the function addition will
09/22/2025 16
not have any effect in the values of x and y outside it 16
Passing Arguments by Value
09/22/2025 17
17
Passing Arguments by Value
Output:
In main the value of whole is 12
and the value of real is 3.5
In changeThem the value of i is changed to 100
and the value of d is changed to 27.5
Now back in main again, the value of whole is still 12
and the value of real is still 3.5
Even though the parameters i and d are changed in the function
changeThem, the arguments whole and real are not modified.
The parameters i and d only contain copies of whole and real.
The changeThem function does not have access to the original
arguments.
09/22/2025 18
18
Passing Arguments by Reference
There might be some cases where you need to manipulate from inside
a function the value of an external variable.
For that purpose we can use arguments passed by reference
A reference variable is an alias for another variable.
When used as a parameter, a reference variable allows a function to
access the parameter’s original argument.
Any change to the parameter is actually made to the original
argument.
When we use a reference variable as a parameter, it becomes an alias
for the corresponding variable in the argument list.
Any change made to the parameter is actually made to the variable in
the calling function.
When data is passed to a parameter in this manner, the argument is
said to be passed by reference.
09/22/2025 19
Computer Science Dept (Ambo University) 19
Passing Arguments by Reference
Reference variables are defined like regular variables, except there is
an ampersand (&) in front of the name.
For example, the following function definition makes the parameter
refVar a reference variable:
void doubleNum(int &refVar)
{
refVar *= 2;
}
Note: The variable refVar is called “a reference to an int.”
Note: The ampersand must appear in both the prototype and the
header of any function that uses a reference variable as a parameter.
It does not appear in the function call.
09/22/2025 20
Computer Science Dept (Ambo University) 20
Passing Arguments by Reference
Output:
In main, value is 4
Now calling doubleNum...
Now back in main, value is 8
09/22/2025 21
Computer Science Dept (Ambo University) 21
Returning a Boolean Value
Functions may return true or false values.
Output:
Enter an integer and I will tell you if it is
even or odd: 5[Enter]
5 is odd.
09/22/2025 22
22
Scope of variables
All the variables that we intend to use in a program must have been
declared with its type specifier in an earlier point in the code, like we
did in the previous code at the beginning of the body of the function
main when we declared that val is of type int.
A variable can be either of
global scope or
local scope
A global variable is a variable declared in the main body of the source
code, outside all functions, while a local variable is one declared
within the body of a function or a block.
The scope of local variables is limited to the block enclosed in braces
({}) where they are declared.
For example, if they are declared at the beginning of the body of a
function (like in function main) their scope is between its declaration
point and the end of that function.
We’ll look at them in the next slides
09/22/2025 23
Computer Science Dept (Ambo University) 23
Local Variables
Variables defined inside a function are local to that function.
They are hidden from the statements in other functions, which
normally cannot access them. Because the variables defined in a
function are hidden, other functions may have separate, distinct
variables with the same name.
Output:
In main, num is 1
In anotherFunction, num is 20
Back in main, num is still 1
09/22/2025 24
Computer Science Dept (Ambo University) 24
Global Variables
Although local variables are safely hidden from other functions, they
do not provide a convenient way of sharing data.
When large amounts of data must be accessible to all the functions in
a program, global variables are an easy alternative.
A global variable is any variable defined outside all the functions in a
program
The scope of a global variable is the portion of the program from the
variable definition to the end.
A local variable is defined inside a function and is not accessible
outside the function.
A global variable is defined outside all functions and is accessible to
all functions in its scope.
Unless you explicitly initialize numeric global variables, they are
automatically initialized to zero.
Global character variables are initialized to NULL.
09/22/2025 25
Computer Science Dept (Ambo University) 25
Global Variables
Output:
In main, num is 2
In anotherFunction, num is 2
But, it is now changed to 50
Back in main, num is 50
09/22/2025 26
Computer Science Dept (Ambo University) 26
Overloading Functions
C++, however, allows you to overload function names. That means
you may assign the same name to multiple functions, as long as their
parameter lists are different. The compiler will use the parameter lists
to distinguish between them.
Output:
36
32
09/22/2025 27
Computer Science Dept (Ambo University) 27
Overloading Functions
The two functions with the same name will differ at least in one of the
following.
The number of parameters
The data type of parameters
The order of appearance
Output:
Square of a given number: 16
Product of two whole numbers: 42
09/22/2025 28
Computer Science Dept (Ambo University) 28
Different Function Categories we have seen so far
Function with no return value and no argument.
void add(void);
Function with arguments passed and no return value.
void add(int,int);
Function with no arguments but returns a value.
int add(void);
Function with arguments and returns a value.
int add(int,int);
09/22/2025 29
Computer Science Dept (Ambo University) 29
Function with no return value and no argument
int main()
{
void disp(void); //prototype
disp(); //caller function
return 0; No arguments
No value passed
returned from } from caller to
calle to caller void disp() //calle function calle
function
{
cout<<“--------”<<endl;
}
09/22/2025 30
Computer Science Dept (Ambo University) 30
Function with arguments passed and no return value
void add(int, int);
int main()
{
int a, b;
cout << “enter values of a and b” << endl;
cin >> a >> b;
add(a, b);
argument return 0;
passed but }
no return void add(int x, int y)
value.
{
int c;
c = x + y;
cout << “addition is” << c;
}
09/22/2025 31
Computer Science Dept (Ambo University) 31
Function with no arguments but returns a value
int main()
{
int add(void);
int z;
z = add();
cout << "sum of 2 nos is " << z;
return 0;
no }
argument int add(void)
but returns {
a value
int a, b;
cout << "enter 2 numbers ";
cin >> a >> b;
return( a + b );
}
09/22/2025 32
Computer Science Dept (Ambo University) 32
Function with arguments and returns a value
int main ()
{
int sqr(int); //function prototype
int a, ans;
cout << "Pls enter a number" << endl;
cin >> a;
ans = sqr(a); //function call
pass cout << "square of " << a <<" is " << ans;
argument return 0;
and returns }
a value
int sqr(int X) //function declaratory/definition
{
return(X * X);
}
09/22/2025 33
Computer Science Dept (Ambo University) 33
C++ function call by pointer
The call by pointer method of passing arguments to a function copies
the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual
argument used in the call.
This means that changes made to the parameter affect the passed
argument.
To pass the value by pointer, argument pointers are passed to the
functions just like any other value.
So accordingly you need to declare the function parameters as
pointer types as in the following function swap(), which exchanges
the values of the two integer variables pointed to by its arguments.
09/22/2025 34
Computer Science Dept (Ambo University) 34
C++ function call by pointer
void Swap1 (int x, int y) // pass-by-value (objects)
{
int temp = x;
x = y;
y = temp;
}
void Swap2 (int *x, int *y) // pass-by-value (pointers)
{
int temp = *x;
*x = *y;
*y = temp;
}
void Swap3 (int &x, int &y) // pass-by-reference
{
int temp = x;
x = y;
y = temp;
}
09/22/2025 35
Computer Science Dept (Ambo University) 35
C++ function call by pointer
Although Swap1 swaps x and y, this has no effect on the arguments
passed to the function, because Swap1 receives a copy of the
arguments.
• What happens to the copy does not affect the original.
Swap2 overcomes the problem of Swap1 by using pointer parameters
instead.
• By dereferencing the pointers, Swap2 gets to the original values
and swaps them.
Swap3 overcomes the problem of Swap1 by using reference
parameters instead.
• The parameters become aliases for the arguments passed to the
function and therefore swap them as intended.
• Swap3 has the added advantage that its call syntax is the same as
Swap1 and involves no addressing or dereferencing.
• The following main function illustrates the differences:
09/22/2025 36
Computer Science Dept (Ambo University) 36
C++ function call by pointer
int main ()
{
int i = 10, j = 20;
Swap1(i, j);
cout << i << ", " << j << '\n';
Swap2(&i, &j);
cout << i << ", " << j << '\n';
Swap3(i, j);
cout << i << ", " << j << '\n';
}
• When run, it will produce the following output:
10, 20
20, 10
10, 20
09/22/2025 37
Computer Science Dept (Ambo University) 37
Default Arguments
Default values are specified when the function is declared.
Compiler looks at the prototype to see how many arguments function
uses.
Default arguments are useful in situations here some arguments
always have the same value.
A default argument is a value provided in a function declaration that
is automatically assigned by the compiler if the caller of the function
doesn’t provide a value for the argument with a default value.
In case any value is passed the default value is overridden.
Example: Look at the next slides.
09/22/2025 38
Computer Science Dept (Ambo University) 38
Default Arguments
int sum(int x, int y, int z = 0, int w = 0)
{
return (x + y + z + w); OUTPUT:
} 25
int main() 50
{ 80
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
Explanation: In statement 1, only two values are passed, hence the variables z and
w take the default values as 0.
In statement 2, three values are passed, so the value of z is over-ridden with 25.
In statement 3, four values are passed, so the value of z and w are over-ridden with
25 and 30 respectively.
09/22/2025 39
Computer Science Dept (Ambo University) 39
THE END
THANK YOU!
«NEXT»
ΞFunctions
Computer Science Dept (Ambo University) 40