Object Oriented Programming Lab 4
Object Oriented Programming Lab 4
EE DEPARTMENT
Experiment # 04
Functions, Function overloading
Objective
The objective of this lab is to get the understanding of function, types of functions and define user
functions and return value from function
Software Tools
1. Microsoft Visual Studio
Theory
1. Function
A function is a subprogram that acts on data and often returns a value. A program written with
numerous functions is easier to maintain, update and debug than one very long program. By
programming in a modular (functional) fashion, several programmers can work independently on
separate functions which can be assembled at a later date to create the entire project. Each
function has its own name. When the function is finished, execution returns to the area of the
program code from which it was called, and the program continues on to the next line of code.
EE DEPARTMENT
// function definition
void starline()
{
int count; // declaring a LOCAL variable
for(count = 1; count <=65; count++)
cout<< "*";
cout<<endl;
}
Argument to a function
Sometimes the calling function supplies some values to the called function. These are known as
parameters. The variables which supply the values to a calling function called actual parameters.
The variable which receive the value from called statement are termed formal parameters.
#include<iostream>
using namespace std;
void area(float);
int main()
{
float radius;
cin>>radius;
area(radius);
return 0;
EE DEPARTMENT
}
void area(float r)
{
cout<< “the area of the circle is”<<3.14*r*r<<”\n”;
}
Here radius is called actual parameter and r is called formal parameter.
//timesTwo function
int timesTwo (int num)
{
int answer; //local variable
answer = 2 * num;
return (answer);
}
Calling of a function
The function can be called using either of the following methods:
i) call by value
ii) call by reference
Call by value
In call by value method, the called function creates its own copies of original values sent to it.
Any changes, that are made, occur on the function’s copy of values and are not reflected back to
the calling function.
EE DEPARTMENT
Call by reference
In call be reference method, the called function accesses and works with the original values using
their references. Any changes, that occur, take place on the original values are reflected back to
the calling code.
Consider the following program which will swap the value of two variables.
#include<iostream> #include<iostream>
using namespace std; using namespace std;
void swap(int &, int &); void swap(int , int );
int main() int main()
{ {
int a=10,b=20; int a=10,b=20;
swap(a,b); swap(a,b);
cout<<a<<" "<<b; cout<<a<<" "<< b;
return 0; return 0;
} }
void swap(int &c, int &d) void swap(int c, int d)
{ {
int t; int t;
t=c; t=c;
c=d; c=d;
d=t; d=t;
} }
output: output:
20 10 10 20
EE DEPARTMENT
average = result(60,70);
passes the value 60 to marks1, 70 to marks2 and lets the function use default value of 75 for
marks3.
The function call
average = result(60,70,80);
passes the value 80 to marks3.
Inline function
Functions save memory space because all the calls to the function cause the same code to be
executed. The functions body need not be duplicated in memory. When the complier sees a
function call, it normally jumps to the function. At the end of the function, it normally jumps back
to the statement following the call.
While the sequence of events may save memory space, it takes some extra time. To save
execution time in short functions, inline function is used. Each time there is a function call, the
actual code from the function is inserted instead of a jump to the function. The inline function is
used only for shorter code.
inline int cube(int r)
{
return r*r*r;
}
Some important points to be noted
Function is made inline by putting a word inline in the beginning.
Inline function should be declared before main() function.
It does not have function prototype.
Only shorter code is used in inline function. If longer code is made inline then compiler ignores
the request and it will be executed as normal function.
EE DEPARTMENT
all functions which follow their declaration. So it should be defined at the beginning, before any
function is defined.
Unary scope resolution operator (::) It is possible to declare local and global variables of the
same name. C++ provides the unary scope resolution operator (::) to access a global variable
when a local variable of the same name is in scope. A global variable can be accessed directly
without the unary scope resolution operator if the name of the global variable is not the same as
that of a local variable in scope.
2. Function overloading
Function overloading allows you to use the same name for different functions.Function
overloading is usually used to enhance the readability of the program. If you have to perform one
single operation but with different number or types of arguments, then you can simply overload
the function.
Ways to overload a function
1. By changing number of Arguments.
2. By having different types of argument.
EE DEPARTMENT
int main()
{
sum (10,20);
sum(10.5,20.5);
}
EE DEPARTMENT
Lab Tasks
1. Write a function that takes two integers and returns the larger one.
2. Write a function that takes username and password as string and display message "login in
success" if username is "abcd" and password is="pakistan" and display message "Login
failed" otherwise.
3. Write a function that swaps two numbers void swap() using reference parameters. Swap
two numbers without using third variable.
4. Write a C++ program with a function int find_char(char*,char)
That finds out whether there is a given character in a string. The return value of the
function find_char() should be 1 if the given character is in the string otherwise 0.
5. Write a function to add 10 in each element of an array.
6. Overload the function in Question 1 with Different number of arguments.
7. Overload the function in Question 3 with Different data types of arguments.