Unit-3 C++ Functions: 2140705 Object Oriented Programming With C++
Unit-3 C++ Functions: 2140705 Object Oriented Programming With C++
Object Oriented
Programming with C++
Unit-3
C++ Functions
Prof. Rupesh G. Vaishnav
9428037452
[email protected]
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
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 33
C++ Function – (Cont…)
There are three elements of user defined function
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 44
Simple Function – (Cont…)
Function Declaration
Syntax:
return-type function-name (arg-1, arg 2, …);
Example: int addition(int , int );
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 arguments/
parameters
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-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 66
Categories of Function (Cont…)
(2) Function with arguments but no return value
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 77
Categories of Function (Cont..)
(3) Function with no argument but returns value
int func1();
void main()
{
....
I like C++ so much
int z = func1();
}
int func1()
I like Rupesh sir
{
....
return 99;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 88
Categories of Function (Cont…)
(4) Function with no argument and no return value
void func1();
void main()
{
....
func1();
I like C++ so much
}
void func1()
I like Rupesh sir
{
....
....
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 99
Program: Categories of function
Write C++ programs to demonstrate various categories of
function, Create function addition for all categories.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 10
10
Function with argument and returns value
#include <iostream> Value of
using namespace std; int main() Argument int fun1(int f)
{ {
..... .....
int add(int, int); b = fun1(a); .....
..... return e;
int main(){
int a=5,b=6,ans; I like C++ so much
} Function
Result
}
ans = add(a,b);
I like Rupesh sir
cout<<"Addition is="<<ans;
return 0;
}
int add(int x,int y)
{
return x+y;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 11
11
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-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 12
12
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
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 14
14
Categories of Functions Summary
(1) Function with argument and returns value
Value of
int main() Argument int fun1(int f)
{ {
..... .....
b = fun1(a); .....
..... return e;
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
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 15
15
Categories of Functions Summary
(3) Function with no argument and returns value
No
int main() Input int fun1()
{ {
..... .....
b = fun1(); .....
..... return e;
I like C++ so much
} Function
Result
}
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-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 16
16
Call by Reference
Call 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 body, the reference is used to access the actual
argument used in the call.
Actual Parameters
I like C++ so much
int main(){
add(a,b);
I like Rupesh
}
sir Formal Parameters
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 19
19
Program: Solution
void swapptr(int *x, int *y)
{
int z = *x; Pointers as arguments
*x=*y;
References as
*y=z;
arguments
}
I like C++ so much
void swapref(int &x, int &y)
{
int z = x;
I like Rupesh sir
int main()
x = y; {
y = z; ...
} swapptr(&a,&b);
swapref(a,b);
...
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 20
20
Program: Solution
void swapptr(int *, int *);
void swapref(int &, int &);
int main()
{
int a = 45;
int b = 35;
cout<<"Before Swap\n";
cout<<"a="<<a<<" b="<<b<<"\n";
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;
}
void swapref(int
I like&x,C++ so
int &y)
much
{
int z = x;
I likeOUTPUT
Rupesh sir
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-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 22
22
Program: Return by Reference
Write a C++ program to return reference of maximum of two
numbers from function max.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 23
23
Program: Solution
int& max(int &, int &);
int main() Function declaration
{ returning reference
int a=5,b=6,ans;
ans = max(a,b);
I like C++ so much
cout<<"Maximum="<<ans;
}
I like Rupesh sir
int& max(int &x,int &y)
{
if (x>y)
return x;
else
return y;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 24
24
Program: Returning Reference
int x; setx() is declared with a
int& setdata(); reference type,
int&
int main()
as the return type:
{ int& setx();
setdata() = 56;
cout<<"Value="<<x; I like C++ so much
This function contains
return x;
You can put a call to this function
}
return 0; I like Rupesh sir
on the left side of the equal sign:
int& setdata() setx() = 92;
The result is that the variable
{ returned by the function is
return x; assigned the value on the right
} side of the equal sign.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 25
25
C Preprocessors
Macros
C Preprocessors Macros
C Preprocessor is a text substitution in program.
It instructs the compiler to do pre-processing before the actual
compilation.
All preprocessor commands begin with a hash symbol (#).
I like C++ so much
I like Rupesh sir
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 27
27
C Preprocessor Macro Example
#include <stdio.h>
#define PI 3.1415
Preprocessor
#define circleArea(r) (PI*r*r)
int main()
{
int radius;
float area;
I like C++ so much
I like Rupesh sir
printf("Enter the radius: ");
scanf("%d", &radius);
area = circleArea(radius);
printf("Area = %f", area);
return 0; Every time the program encounters
} circleArea(argument), it is replaced by
(3.1415*(argument)*(argument)).
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 28
28
Inline Functions
Inline Functions
Every time a function is called it takes a lot of extra time to execute
series of instructions such as
1. Jumping to the function
2. Saving registers
I like C++ so much
3. Pushing arguments into stack
4. Returning to the calling function
If a function Ibody
like Rupesh
is small sirtime is more than actual
then overhead
code execution time so it becomes more time consuming.
Preprocessor macros is a solution to the problem of small functions
in C.
In C++, inline function is used to reduce the function call overhead.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 30
30
Inline Functions (Cont…)
Syntax:
inline return-type function-name(parameters)
{
// function code
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 32
32
Program: Solution
#include <iostream>
using namespace std;
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 33
33
Critical situations Inline Functions
Some of the situations inline expansion may not work
1) If a loop, a switch or a goto exists in function body.
2) If function is not returning any value.
3) If function contains static variables.
I like C++ so much
4) If function is recursive.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 34
34
Function Overloading
Function Overloading
Suppose we want to make functions that add 2 values, add 3
values , add 4 values
In C Function with
int sum(int a, int b); same name in a
I like C++ so much
int sum(int a, int b, int c);
int sum(int a, int b, int c, int d);
program is not
allowed in C
I like Rupesh sir language
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 37
37
Function Overloading
int sum(int a, int b); Valid
I like
int sum(float Rupesh
b, int a); sir Valid
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 38
38
Program: Function overloading
Write a C++ program to demonstrate function overloading. Create
function display() with different arguments but same name
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 39
39
Program: Solution (Cont…)
void display(int var)
{
cout << "Integer number: " << var << endl;
}
void display(float var)
{ I like C++ so much
cout << "Float number: " << var << endl;
} I like Rupesh sir
void display(int var1, float var2) {
cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 40
40
Program: Solution
int main()
{
int a = 5; float b = 5.5;
display(a);
display(b);
display(a, b); I like C++ so much
return 0;
} I like Rupesh sir
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 41
41
Program: Function overloading
Write a C++ program to demonstrate function overloading. Create
function area() that calculates area of circle, triangle and box.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 42
42
float area(int r)
{ Program #7
return 3.14*r*r; Solution
}
float area(int h, int b)
{
return 0.5*h*b;
}
I like C++ so much
float area(int l, int w, int h)
{
return l*w*h; I like Rupesh sir
}
int main(){
cout<<"area of circle="<<area(5);
cout<<“\n area of triangle="<<area(4,9);
cout<<“\n area of box="<<area(5,8,2);
return 0;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 43
43
Default Function
Arguments
Default Function Argument
5% 20%
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 48
48
Common Mistakes
(1) void add(int a, int b = 3, int c, int d = 4);
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 49
49
Program: Default Arguments
Write a C++ program to create function sum(), that performs
addition of 3 integers also demonstrate Default Arguments
concept.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 50
50
Program: Default Arguments
#include <iostream>
using namespace std;
int sum(int x, int y=10, int z=20)
{
return (x+y+z);
} I like C++ so much
int main()
{ I like Rupesh sir
cout << "Sum is : " << sum(5) << endl;
cout << "Sum is : " << sum(5,15) << endl;
cout << "Sum is : " << sum(5,15,25) << endl;
return 0;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 51
51
Thank You