0% found this document useful (0 votes)
277 views52 pages

Unit-3 C++ Functions: 2140705 Object Oriented Programming With C++

The document discusses different categories of functions in C++. It explains that a function is a block of code that performs a task. There are four categories of functions: 1) functions with arguments and a return value, 2) functions with arguments but no return value, 3) functions without arguments but with a return value, 4) functions without arguments or a return value. It provides examples of functions from each category and a program to demonstrate creating addition functions for all categories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
277 views52 pages

Unit-3 C++ Functions: 2140705 Object Oriented Programming With C++

The document discusses different categories of functions in C++. It explains that a function is a block of code that performs a task. There are four categories of functions: 1) functions with arguments and a return value, 2) functions with arguments but no return value, 3) functions without arguments but with a return value, 4) functions without arguments or a return value. It provides examples of functions from each category and a program to demonstrate creating addition functions for all categories.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

2140705

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

I like C++ so much


Library Function User Defined Function
I like Rupesh sir
Predefined Created by User
Declarations inside header files Programmer need to declare it
Eg. printf() – stdio.h Eg. factorial()
pow() – cmath.h areaofcircle()
strcmp() – cstring.h

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

void func1(); Function Declaration


void main()
{
....
func1(); I like C++ soFunction
much call
}
void func1() I like Rupesh sir
{
Function
.... Function
definition
.... body
}

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

Return type int func1(int , int ); \\declaration


void main()

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

void func1(int , int ); \\function declaration


void main()
{
.... I like C++ so much
func1(5,6); \\function call
} I like Rupesh sir
void func1(int a, int b) \\function definition
{
....
....
}

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.

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 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
}

I like Rupesh sir


cout<<"Addition is="<<ans;
return 0;
}
void add()
{
int a=5,b=6;
return a+b;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 13
13
Function with no argument and no return value
void add();
No
int main() Input void fun1()
int main() { {
{ ..... .....
fun1(); .....
add(); ..... .....
return 0; I like C++ so much
} No Return
value
}
}
void add() I like Rupesh sir
{
int a=5,b=6;
cout<<"Addition is="<<a+b;
}

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

void add(int x,int y){


cout << x+y;
Note: }
 Actual parameters are parameters as they appear in function calls.
 Formal parameters are parameters as they appear in function
declarations / definition.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 18
18
Program: Swap using pointer, reference
 Write a C++ program that to swap two values using function
1. With pass by pointer
2. With pass by reference

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 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.

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 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
}

I like C++ so much


 Add inline word before the function definition to convert simple
function to inline function.
Example: I like Rupesh sir
inline 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 31
31
Program: Inline function
 Write a C++ program to create inline function that returns cube of
given number (i.e n=3, cube=(n*n*n)=27).

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 32
32
Program: Solution
#include <iostream>
using namespace std;

inline int cube(int s)


{
return s*s*s; I like C++ so much
}
int main() I like Rupesh sir
{
cout << "The cube of 3 is: " << cube(3);
return 0;
}
 Calls inline function cube with argument 3

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.

I like Rupesh sir

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

In C++ Function with


int sum(int a, int b); same name in a
int sum(int a, int b, int c); program is
int sum(int a, int b, int c, int d); allowed in C++
language
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 36
36
Function overloading – Cont…
 C++ provides function overloading which allows to use multiple
functions sharing the same name .
 Function overloading is also known as Function Polymorphism in
OOP.

I like C++ so much


 It is the practice of declaring the same function with different
signatures.
 However, theI two
like Rupesh
functions sir name must differ in at
with the same
least one of the following, Arguments
a) The number of arguments make the
b) The data type of arguments function
c) The order of appearance of arguments unique

 Function overloading does not depends on return type.

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

float sum(int a, int b); Invalid

int sum(int a, int c); Invalid

I like C++ so much


int sum(int a, float b); Valid

I like
int sum(float Rupesh
b, int a); sir Valid

float sum(float a, float b); Valid

int sum(int a, int b, int c); Valid

int sum(int a, float b, int c); 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

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 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.

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 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%

int cubevolume(int l=5, int w=6, int h=7)


{ I like C++ so much
return l*w*h;
} I like Rupesh sir
int main() Here, there can be
If argument fourspecified
is not types ofthen,
function
{ compiler lookscalls possible to see how
at declaration
cubevolume(); many arguments a function uses and
cubevolume(9);
alert program to use default values
cubevolume(15,12);
cubevolume(3,4,7);
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 45
45
Default Argument Example
int volume(int l=5,int w=6, int h=7)
{
return l*w*h;
}
int main() {
I like C++ so much
cout<<"volume="<<volume()<<endl;
cout<<"volume="<<volume(9)<<endl;
I like Rupesh sir
cout<<"volume="<<volume(15,2)<<endl;
cout<<"volume="<<volume(3,4,7)<<endl;
return 0;
}
 Function call passing
withoutall
only
passing
two oneargument.
argument.
arguments.
arguments.
 Explicitly value5,6,7
Default value 9
3,4,7
15,2
passed to l.totol,w
considered
passed
passed l,
l,w,h
for w, h respectively.
respectively.
respectively.
 Default value 6,7
7 considered
considered h w,h
for for respectively.
respectively.
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 46
46
Default Arguments
 while invoking a function If the argument/s are not passed then,
the default values are used.
 We must add default arguments from right to left.
 We cannot provide a default value to a particular argument in the
I like C++ so much
middle of an argument list.
 Default arguments are useful in situations where some arguments
always have Ithelike Rupesh sir
same value.

int cubevolume( int l = 2, int w = 2, int h = 2 )


{
return l*w*h;
}
Unit-3
Unit-3 C++
C++ Functions
Functions Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology 47
47
Default Arguments (Cont…)
 Legal and illegal default arguments
void f(int a, int b, int c=0); Valid
void f(int a, int b=0, int c=0); Valid
void f(int a=0, int b, int c=0); Invalid
I like C++ so much
void f(int a=0, int b, int c); Invalid
void f(int a=0, int b=0, int c=0); Valid
I like Rupesh sir

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);

 You cannot miss a default argument in between two arguments.


 In this case, c should also be assigned a default value.

I like C++ so much


(2) void add(int a, int b = 3, int c, int d);
I like Rupesh sir
 If you want a single default argument, make sure the argument is
the last one.

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.

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 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

You might also like