Object Oriented Programming
Introduction to Function
In C++, a function is a group of statements that is given name, and
which can be called from some point of the program. Advantage of using
functions is to reduce the size of the program by calling and using them at
different places in the program.
FUNCTION DEFINITION AND DECLARATION
The general syntax of a function definition in C++ is shown below:
Syntax:
Return_Type name_of_the_function (argument list)
//body of the function
Here, the type specifies the type of the value to be returned by the
function. It may be any valid C++ data type. When no type is given, then
the compiler returns an integer value from the function.
Name_of_the_function is a valid C++ identifier (no reserved word
allowed) defined by the user and it can be used by other functions for
calling this function.
Argument list is a comma separated list of variables of a function
through which the function may receive data or send data when called from
other function.
void add() //function definition
{ //function
int a,b,sum; declaration
cout<<”Enter two integers”<<endl;
Void add();
cin>>a>>b;
//function
sum=a+b;
calling
cout<<”\nThe sum of two numbers is “<<sum<<endl;
add();
}
Shri S.V.Patel College of CS & BM Page 1
Object Oriented Programming
PASSING ARGUMENTS TO A FUNCTION
When calling a function, the exact mechanism for passing
arguments (assigning arguments to parameters) depends on the evaluation
strategy and how it is implemented. Some common ways of passing
arguments to a function are:
call by value
call by reference
1. Call by value:
The call by value method of passing arguments to a function copies
the actual value of an argument into the formal parameter of the function.
In this case, changes made to the parameter inside the function have no
effect on the argument.
By default, C++ uses call by value to pass arguments. In general, this
means that code within a function cannot alter the arguments used to call
the function.
void swap(int x, int y)
{
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put x into y */
}
Shri S.V.Patel College of CS & BM Page 2
Object Oriented Programming
Example: (swap.cpp)
//swapping of two numbers using call by value
#include<iostream.h>
#include<conio.h>
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{ clrscr();
int a=100;
int b=200;
cout<<"before swap"<<a<<"\n";
cout<<"before swap b"<<b<<"\n";
swap(a,b);
cout<<"after swap a"<<a<<"\n";
cout<<"after swap b"<<b;
getch();
return 0;
}
2. Pass by reference:
The call by reference method of passing arguments to a
function copies the reference of an argument into the formal
parameter. Inside the function, the reference 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 reference, argument reference is passed to
the functions just like any other value.
Shri S.V.Patel College of CS & BM Page 3
Object Oriented Programming
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
}
Example:
// swapping of two variable using call by reference. (swap1.cpp)
#include<iostream.h>
#include<conio.h>
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{ clrscr();
int a=100;
int b=200;
cout<<"before swap"<<a<<"\n";
cout<<"before swap b"<<b<<"\n";
swap(a,b);
cout<<"after swap a"<<a<<"\n";
cout<<"after swap b"<<b;
getch();
return 0;
}
Shri S.V.Patel College of CS & BM Page 4
Object Oriented Programming
Inline Function:
One of the objectives of using programming is to save memory space,
when a function is likely to be called many times. Every time a function is
called, it tasks a lot of extra time in executing a series of instruction like
jumping to the function, pushing arguments into stack, and returning to the
calling function.
Solution for this problem is to use macro. Major drawback with a
macro is error checking does not occur during compilation time.
C++ inline function is powerful concept that is commonly used with
classes. If a function is inline, the compiler places a copy of the code of that
function at each point where the function is called at compile time.
An inline function is a function which is expanded in line when
it is invoked (called). The compiler replaces the function call with
the corresponding function code.
Any change to an inline function could require all clients of the function to be
recompiled because compiler would need to replace all the code once again
otherwise it will continue with old functionality.
To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function. The compiler
can ignore the inline qualifier in case defined function is more than a line.
Syntax:
inline Function-header
{
// Function body
}
Example:
inline int square(int x)
{
return (a*a);
}
Shri S.V.Patel College of CS & BM Page 5
Object Oriented Programming
Here, inline keyword sends a request to the compiler. The
compiler may ignore this request if the function body is too complicated or
too long and compile the function as a normal function.
The inline function does not work for the following situations :
1. For functions returning values and having a loop or a switch or a
goto statement.
2. For functions that do not return value and having a return
statement.
3. For functions having static variable(s).
4. If the inline functions are recursive (i.e. a function defined in terms
of itself).
The benefits of inline functions are as follows :
1. Better than a macro.
2. Function call overheads are eliminated.
3. Program becomes more readable.
4. Program executes more efficiently.
Example: (Inline_e.cpp)
#include<iostream.h>
#include<conio.h>
inline float mul(float x, float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
void main()
Shri S.V.Patel College of CS & BM Page 6
Object Oriented Programming
{
clrscr();
float a,b,c;
cout<<"enter a and b vlaue";
cin>>a;
cin>>b;
cout<<mul(a,b)<<"\n";
cout<<"enter value of c";
cin>>c;
cout<<cube(c)<<"\n";
getch();
}
DEFAULT ARGUMENTS
C++ allows a function to assign a parameter the default value in case no
argument for that parameter is specified in the function call.
A default argument is checked for type at the time of declaration and
evaluated at the time of call.
Important point is that only trailing arguments can have default values and so
that we must add defaults from right to left.
Example:
float amount(float principal, int duration, float rate=0.12);
Default vales are specified when the function is declared.
We cannot provide a default argument to a particular argument in the middle
of any argument list.
Default argument can be used in that situation where some arguments
always have the same value.
Shri S.V.Patel College of CS & BM Page 7
Object Oriented Programming
int mul (int i, int j=5, int k=10); //legal
int mul (int i = 5, int j); //illegal
int mul (int i=0, int j, int k=10); //illegal
int mul (int i=2, int j=5, int k=10); //legal
Note:
1) If function with default argument is called without passing
arguments, the default arguments are used.
2) If arguments are passed while calling a function, the default
arguments are ignored.
Case 1: no argument is passed Case 2: First argument is passed
int temp (int a=10, float b=0.8); Int temp(int a=10, float b=0.8);
Int main() Int main()
{ {
temp(); temp(20);
} }
Int temp (int a, float b) Int temp (int a, float b)
{ {
} }
Case 3: All arguments are passed Case 4: Second argument is
passed
Int temp(int a=10, float b=0.8);
Int main() Int temp(int a=10, float b=0.8);
{ Int main()
temp(15,2.6); {
} temp(3.5);
3 will be store in a and
}
0.8 assigned to b.
Int temp (int a, float b)
{ Int temp (int a, float b)
{
}
}
Shri S.V.Patel College of CS & BM Page 8
Object Oriented Programming
#include<iostream.h>
#include<conio.h>
void temp(int a=10 , float b=0.8);
int main()
{
clrscr();
//case 1: no argument is passed
temp();
//case 2: first argument is passed
temp(20);
//case 3: all arguments are passed
temp(15,2.5);
//case 4: second argument is passed
temp(3.5);
getch();
return 0;
}
void temp(int p, float q)
{
cout<<p<<"\n";
cout<<q<<"\n";
}
Shri S.V.Patel College of CS & BM Page 9
Object Oriented Programming
//write a program for default argument
#include<iostream.h>
#include<conio.h>
int sum(int a, int b=10, int c=20);
int main()
{
clrscr();
// in this case value 1 is assign to a
// values of b and c are taken from default arguments
cout<<sum(1)<<"\n";
//in this case a is assign to 1 and b is assign to 2
// value of c is taken from default argumnet
cout<<sum(1,2)<<"\n";
// in this case all values are passed during functioncall
//so that default arguments are ignored.
cout<<sum(1,2,3)<<"\n";
getch();
return 0;
}
int sum(int a, int b, int c)
{
int z;
z = a+b+c;
return z;
}
Shri S.V.Patel College of CS & BM Page 10
Object Oriented Programming
Function Overloading:
Overloading means to refer the use of the same thing for different purpose.
In C++, two or more functions can share the same name as long as their
parameter declarations are different. This means that we can use the same
function name to create functions that perform a variety of different task.
This is known as function overloading.
In this situation, the functions that share the same name are said to be
overloaded, and the process is referred to as function overloading. Function
overloading means two or more functions can have the same name but either
the number of arguments or the data type of arguments has to be different.
Function
Declaration call
cout<< add(5,
int add(int a, int b); 10);
int add(int a, int b, int c);
cout<< add(15, 10.0);
double add(double x, double
y); cout<< add(12.5, 7.5);
double add(int p, double q); cout<< add(5, 10, 15);
double add(double p, int q); cout<< add(0.75, 5);
Function overloading involves the following steps:
1) The compiler first tries to find out an exact match in which the
types of actual arguments are the same, and use that function.
2) If the exact match is not found, the compiler uses the integer
promotion to the actual arguments.
Char Int long float double
Shri S.V.Patel College of CS & BM Page 11
Object Oriented Programming
Such as,
Char to int
Float to double
3) When either of them fails, the compiler tries to use the built in
conversion to the actual arguments and then uses the function
whose match is unique. If the conversion is possible to have
multiple matches then the compiler will generate an error
message.
Long square(long n) // functions
Double square (double x)
Square(10) // function call
Will cause an error because int argument can be
converted to either long or double therefore creating an
ambiguous situation which square() should be used.
4) If all the steps fails, then the compiler will try the user defines
conversion combination with integral promotion.
Example: (function.cpp)
/* write a program to calculate area of circle, rectangle and triangle using
function overloading */
#include<iostream.h>
#include<conio.h>
float area(float);
int area(int,int);
float area(float, float);
int main()
{
clrscr();
int l,b;
float r,bs,ht;
Shri S.V.Patel College of CS & BM Page 12
Object Oriented Programming
cout<<"enter the value of r";
cin>>r;
cout<<area(r)<<"\n";
cout<<"enter the value of l and b";
cin>>l>>b;
cout<<area(l,b)<<"\n";
cout<<"enter the value of bs and ht";
cin>>bs>>ht;
cout<<area(bs,ht)<<"\n";
getch();
return 0;
}
float area(float r)
{
return (3.14*r*r);
}
int area (int l, int b)
{
return (l * b);
}
float area(float bs, float ht)
{
return((bs*ht)/2);
}
Here, function call first matches the prototype having the same
number and type of argument and then calls appropriate function
call for execution.
Example: (fun1.cpp)
Write a program to find out volume of cube, cylinder and
Shri S.V.Patel College of CS & BM Page 13
Object Oriented Programming
rectangular box using function overloading.
#include<iostream.h>
#include<conio.h>
int volume(int);
double volume(double,int);
long volume (long,int,int);
void main()
{
clrscr();
cout<<"Volume of cube is \t"<<volume(10)<<"\n";
cout<<"Volume of cylinder is \t"<<volume(2.5,8)<<"\n";
cout<<"Volume of rectangular is \t"<<volume(100L,75,15)<<"\n";
getch();
}
int volume(int a)
{
return(a*a*a);
}
double volume(double r, int h)
{
return(3.14*r*r*h);
}
long volume(long l, int b, int h)
{
return(l*b*h);
}
Example: (fun2.cpp)
Shri S.V.Patel College of CS & BM Page 14
Object Oriented Programming
#include<iostream.h>
#include<conio.h>
int sum(int num1, int num2);
float sum(float num1, int num2, int num3);
void mian()
{
clrscr();
cout<<"sum of two int value is \t"<<sum(10,20);
cout<<"sum ofthree paramter is \t"<<sum(10.20,20,30);
getch();
return 0;
}
int sum(int a, int b)
{
return a+b;
}
float sum(float a,int b, int c)
{
return a+b+c;
}
Shri S.V.Patel College of CS & BM Page 15