Presentations PPT 3
Presentations PPT 3
C++ Functions
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 arguments/
parameters
int func1();
void main()
{
....
int z = func1();
}
int func1()
{
....
return 99;
}
Categories of Function (Cont…)
(4) Function with no argument and no return value
void func1();
void main()
{
....
func1();
}
void func1()
{
....
....
}
Program: Categories of function
Write C++ programs to demonstrate various categories of
function, Create function addition for all categories.
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(){ } Function }
int a=5,b=6,ans; Result
ans = add(a,b);
cout<<"Addition is="<<ans;
return 0;
}
int add(int x,int y)
{
return x+y;
}
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() ..... .....
{ } No Return }
int a=5,b=6; value
add(a,b);
return 0;
}
void add(int x,int y)
{
cout<<"Addition is="<<x+y;
}
Function with no argument but returns value
int add(); No
int main() Input int fun1()
{ {
int main() ..... .....
{ b = fun1(); .....
int ans; ..... return e;
} Function }
ans = add(); Result
cout<<"Addition is="<<ans;
return 0;
}
void add()
{
int a=5,b=6;
return a+b;
}
Function with no argument and no return value
void add();
No
int main() Input void fun1()
int main() { {
{ ..... .....
fun1(); .....
add(); ..... .....
return 0; } No Return }
} value
void add()
{
int a=5,b=6;
cout<<"Addition is="<<a+b;
}
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;
} Function }
Result
(2) Function with argument and but no return value
Value of
int main() Argument void fun1(int f)
{ {
..... .....
fun1(a); .....
..... .....
} No Return }
value
Categories of Functions Summary
(3) Function with no argument and returns value
No
int main() Input int fun1()
{ {
..... .....
b = fun1(); .....
..... return e;
} Function }
Result
(4) Function with no argument and but no return value
No
int main() Input void fun1()
{ {
..... .....
fun1(); .....
..... .....
} No Return }
value
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
int main(){
add(a,b);
} Formal Parameters
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 &x, int &y)
{
int z = x; OUTPUT
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
Program: Return by Reference
Write a C++ program to return reference of maximum of two
numbers from function max.
Program: Solution
int& max(int &, int &);
int main() Function declaration
{ returning reference
int a=5,b=6,ans;
ans = max(a,b);
cout<<"Maximum="<<ans;
}
int& max(int &x,int &y)
{
if (x>y)
return x;
else
return y;
}
Program: Returning Reference
int x; setx() is declared with a
int& setdata(); reference type,
int main() int&
as the return type:
{ int& setx();
setdata() = 56; This function contains
cout<<"Value="<<x; return x;
return 0; You can put a call to this function
} 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.
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 (#).
C Preprocessor Macro Example
#include <stdio.h>
#define PI 3.1415
Preprocessor
#define circleArea(r) (PI*r*r)
int main()
{
int radius;
float area;
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)).
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
3. Pushing arguments into stack
4. Returning to the calling function
If a function body is small then overhead time is more than actual
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.
Inline Functions (Cont…)
Syntax:
inline return-type function-name(parameters)
{
// function code
}
Add inline word before the function definition to convert simple
function to inline function.
Example:
inline int Max(int x, int y)
{
if (x>y)
return x;
else
return y;
}
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).
Program: Solution
#include <iostream>
using namespace std;
However, the two functions with the same name must differ in at
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