Professional Documents
Culture Documents
Unit 4 - C++ Functions
Unit 4 - C++ Functions
Unit 4 - C++ Functions
Object Oriented
Programming
Unit - 4
C++ Functions
Vinita Shah
Asst. Professor, IT Dept., GCET
C++ Functions
C++ Function
▪ A function is a group of statements that together perform a task.
▪ Functions are made for code reusability and for saving time and
space.
Function
▪ Function Definition
Syntax:
return-type function-name (arg-1, arg 2, …)
{
... Function body
}
Example: int addition(int x, int y)
{
return x+y;
}
Categories of Function
(1) Function with arguments and returns value
Function func1
{ I like C++ so much
....
returns integer value
to variable z
I like Rupesh sir
int z = func1(5,6); \\function call
}
int func1(int a, int b) \\definition
{
....
return a+b;
} returns a+b to calling function
Unit – 4 C++ Functions 7
Categories of Function (Cont…)
(2) Function with arguments but no return value
int func1();
void main()
{
....
int z = func1();
I like C++ so much
}
int func1()
I like Rupesh sir
{
....
return 99;
}
ans = add(a,b);
I like Rupesh sir
cout<<"Addition is="<<ans;
return 0;
}
int add(int x,int y)
{
return x+y;
}
Unit – 4 C++ Functions 12
Function with arguments but no return value
#include <iostream>
Value of
using namespace std; int main() Argument void fun1(int f)
{ {
..... .....
void add(int, int); fun1(a); .....
int main() ..... .....
{ I like C++ so much
} No Return
value
}
int a=5,b=6;
add(a,b); I like Rupesh sir
return 0;
}
void add(int x,int y)
{
cout<<"Addition is="<<x+y;
}
Unit – 4 C++ Functions 13
Function with no argument but returns value
int add(); No
int main() Input int fun1()
{ {
int main() ..... .....
{ b = fun1(); .....
..... return e;
int ans;
ans = add();
I like C++ so much
} Function
Result
}
I like
(2) Function with Rupesh
argument sir value
and but no return
Value of
int main() Argument void fun1(int f)
{ {
..... .....
fun1(a); .....
..... .....
} No Return }
value
I like
(4) Function with Rupesh
no argument sir
and but no return value
No
int main() Input void fun1()
{ {
..... .....
fun1(); .....
..... .....
} No Return }
value
Unit – 4 C++ Functions 17
Call by Reference
No. Call by value Call by reference
Changes made inside the function is Changes made inside the function is
2
not reflected on other functions reflected outside the function also
Hence, valueIchanged
▪ like Rupesh siris reflected inside as
inside the function,
well as outside the function.
swapptr(&a,&b);
cout<<"After Swap with pass by pointer\n";
cout<<"a="<<a<<" b="<<b<<"\n";
swapref(a,b);
cout<<"After Swap with pass by reference\n";
cout<<"a="<<a<<" b="<<b<<"\n";
}
Program: Solution (Cont…)
void swapptr(int *x, int *y)
{
int z = *x;
*x=*y;
*y=z;
} I like C++ so much
void swapref(int &x, int &y)
{ I likeOUTPUT
Rupesh sir
int z = x;
Before Swap
x = y;
a=45 b=35
y = z;
After Swap with pass by pointer
}
a=35 b=45
After Swap with pass by reference
a=45 b=35
Unit – 4 C++ Functions 28
Program: Return by Reference
▪ Write a C++ program to return reference of maximum of two
numbers from function max.
#include <iostream>
using namespace std;
#define PI 3.1415
I like C++ so much
int main()
{ I like Rupesh sir
cout << "Value of PI :" << PI << endl;
return 0;
}
int main()
{ I like C++ so much
int i, j;
I like Rupesh sir
i = 100;
j = 30;
2.
I like C++ so much
Through inline function, the class’s data
members can be accessed.
Whereas macro can’t access the class’s
data members.
In C++, inline may be defined either Whereas the macro is all the time
5.
inside the class or outside the class. defined at the beginning of the program.