Function
Function
(OOP)
(Lecture 9)
2022/2021
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
FUNCTION
2
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
Simple Functions
Our first example demonstrates a simple function whose purpose is to
print a line of 45 asterisks.
The example program generates a table and lines for asterisks are used to
make the table more readable. Here’s the listing for TABLE:
#include <iostream>
starline();
starline();
return 0;
}
//....................................................
//starline()
3
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
//function defintion
void starline() // function
declarator
{
for (int j=0; j<45;j++) // function body
cout<<'*';
cout<<endl;
}
• This program consist of two function: main() and star line(). You’ve
already seen many programs that use main() only.
• What other components are necessary to add a function to the
program?
There are three : function declaration, the calls to the function and
the function definition.
• Figure 2 shows the syntax of the function declaration, function call
and function definition.
4
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
Function Declarations
A function declaration tells the compiler about a function name and how to
call the function. The actual body of the function can be defined separately.
5
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
// function body
}
// function declaration
void greet() {
cout << "Hello World";
}
Here,
int main() {
// calling a function
greet();
6
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
// declaring a function
void greet() {
int main() {
greet();
return 0;
Output
Hello there!
7
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
Function Parameters
int main() {
int n = 7;
return 0;
}
#include <iostream>
// display a number
8
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
int main() {
int num1 = 5;
displayNum(num1, num2);
cout<<endl;
return 0;
Output:-
In the above program, we have used a function that has one int parameter
and one double parameter.
We then pass num1 and num2 as arguments. These values are stored by
the function parameters n1 and n2 respectively.
9
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
Note:
The type of the arguments passed while calling the function must match
with the corresponding parameters defined in the function declaration.
Return Statement
In the above programs, we have used void in the function declaration. For
example,
void displayNumber() {
// code
It's also possible to return a value from a function. For this, we need to
specify the returnType of the function during function declaration.
Then, the return statement can be used to return a value from a function.
For example,
return (a + b);
Here, we have the data type int instead of void. This means that the
function returns an int value.
The code return (a + b); returns the sum of the two parameters as the
function value.
The return statement denotes that the function has ended. Any code after
return inside the function is not executed.
#include <iostream>
10
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
// declaring a function
return (a + b);
int main() {
int sum;
return 0;
Output:
100+78=178
In the above program, the add() function is used to find the sum of two
numbers.
We pass two int literals 100 and 78 while calling the function.
We store the returned value of the function in the variable sum, and then
we print it.
11
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
Notice that sum is a variable of int type. This is because the return value
of add() is of int type.
Function Prototype
In C++, the code of function declaration should be before the function call.
However, if we want to define a function after the function call, we need to
use the function prototype. For example,
// function prototype
void add(int, int);
int main() {
// calling the function before declaration.
add(5, 3);
return 0;
}
// function definition
void add(int a, int b) {
cout << (a + b);
}
12
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
This provides the compiler with information about the function name and
its parameters. That's why we can use the code to call a function before the
function has been defined.
#include <iostream>
// function prototype
int main() {
int sum;
return 0;
// function definition
13
Object Oriented Programing MSc.Ahmed& Abeer
Computer Engineering Department -2 nd Stage Al- Esraa University Collage
return (a + b);
Output
100 + 78 = 178
#include <iostream>
#include <cmath>
int main() {
number = 25.0;
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot<<endl;
return 0;
14