SlideShare a Scribd company logo
1 of 66
Functions
What is function???? 
• Function is a self contained block of statements 
that perform a coherent task of some kind. 
• Every C program can be a thought of the collection 
of functions. 
• main( ) is also a function.
Types of Functions. 
• Library functions 
– These are the in- -built functions of ‘C ’library. 
– These are already defined in header files. 
– e.g. printf( ); is a function which is used to print at 
output. It is defined in ‘stdio.h ’ file . 
• User defined functions. 
– Programmer can create their own function in C to 
perform specific task
Why use functions? 
• Writing functions avoids rewriting of the same code 
again and again in the program. 
• Using function large programs can be reduced to 
smaller ones. It is easy to debug and find out the 
errors in it. 
• Using a function it becomes easier to write program 
to keep track of what they are doing.
Function Declaration 
ret_type func_name(data_type par1,data_type par2); 
Function Defination 
ret_type func_name(data_type par1,data_type 
par2) 
{} 
Function Call 
func_name(data_type par1,data_type par2);
Function prototype 
• A prototype statement helps the compiler to check 
the return type and arguments type of the function. 
• A prototype function consist of the functions return 
type, name and argument list. 
• Example 
– int sum( int x, int y);
Example 
#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
void print(); /*func declaration 
print(); /*func calling 
printf(“no parameter and no return value”); 
print(); 
getch(); 
} 
void print() /*func defination 
{ 
For(int i=1;i<=30;i++) 
{ 
Cout<<“*”; 
} 
Cout<<endl; 
}
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a=10,b=20; 
int sum(int,int); 
int c=sum(a,b); /*actual arguments 
Cout<<“sum is” << c; 
getch(); 
} 
intsum(int x, int y) /*formal arguments 
{ 
int s; 
s=x+y; 
return(s); /*return value 
}
Function parameters 
• The parameters are local variables inside the body 
of the function. 
– When the function is called they will have the values 
passed in. 
– The function gets a copy of the values passed in (we will 
later see how to pass a reference to a variable).
Arguments/Parameters 
• one-to-one correspondence between the 
arguments in a function call and the parameters 
in the function definition. 
int argument1; 
double argument2; 
// function call (in another function, such as main) 
result = thefunctionname(argument1, argument2); 
// function definition 
int thefunctionname(int parameter1, double parameter2){ 
// Now the function can use the two parameters 
// parameter1 = argument 1, parameter2 = argument2
a) Actual arguments:-the arguments of calling 
function are actual arguments. 
b) Formal arguments:-arguments of called function 
are formal arguments. 
c) Argument list:-means variable name enclosed 
within the paranthesis.they must be separated by 
comma 
d) Return value:-it is the outcome of the function. 
the result obtained by the function is sent back to 
the calling function through the return statement.
Return statement 
• It is used to return the value to the calling function. 
• It can be used in fallowing ways 
a) return(expression) return(a+b); 
b) A function may use one or more return statement 
depending upon condition 
if(a>b) 
return(a); 
else 
return(b);
c) return(&a); returns the address of the variable 
d)returns(*p);returns value of variable through the 
pointer 
e)return(sqrt(r)); 
f)return(float (sqrt(2.4)));
Categories of functions 
• A function with no parameter and no return value 
• A function with parameter and no return value 
• A function with parameter and return value 
• A function without parameter and return value
A function with no parameter and no return value 
#include<conio.h> 
void main() 
{ 
clrscr(); 
void print(); /*func declaration 
print(); /*func calling 
Cout<<“no parameter and no return value”; 
print(); 
getch(); 
} 
void print() /*func defination 
{ 
For(int i=1;i<=30;i++) 
{ 
cout<<“*”; 
} 
Cout<<“n”; 
}
A function with no parameter and no return value 
• There is no data transfer between calling and called 
function 
• The function is only executed and nothing is 
obtained 
• Such functions may be used to print some 
messages, draw stars etc
A function with parameter and no return value 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a=10,b=20; 
void mul(int,int); 
mul(a,b); /*actual arguments 
getch(); 
} 
void mul(int x, int y) /*formal arguments 
{ 
int s; 
s=x*y; 
Cout<<“mul is” << s; 
}
A function with parameter and return 
value 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a=10,b=20,c; 
int max(int,int); 
c=max(a,b); 
Cout<<“greatest no is” <<c; 
getch(); 
} 
int max(int x, int y) 
{ 
if(x>y) 
return(x); 
else
A function without parameter and return value 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int a=10,b=20; 
int sum(); 
int c=sum(); /*actual arguments 
Cout<<“sum is”<< c; 
getch(); 
} 
int sum() /*formal arguments 
{ 
int x=10,y=30; 
return(x+y); /*return value 
}
Argument passing techniques 
• Pass By Value 
• Pass By Reference 
• Pass By Pointeraddress
Call By Value 
• It is a default mechanism for argument passing. 
• When an argument is passed by value then the 
copy of argument is made know as formal 
arguments which is stored at separate memory 
location 
• Any changes made in the formal argument are not 
reflected back to actual argument, rather they 
remain local to the block which are lost once the 
control is returned back to calling program
Example 
void main() 
{ 
int a=10,b=20; 
void swap(int,int); 
Cout<<“before function calling”<<a<<b; 
swap(a,b); 
Cout<<“after function calling”<<a<<b; 
getch(); 
}
void swap(int x,int y) 
{ 
int z; 
z=x; 
x=y; 
y=z; 
Cout<<“value is”<<x<<y; 
}
Output: 
before function calling a=10 b=20 
value of x=20 and y= 10 
after function calling a=10 b=20
Call By pointer/address 
• In this instead of passing value, address are passed. 
• Here formal arguments are pointers to the actual 
arguments 
• Hence change made in the argument are 
permanent.
Example 
Void main() 
{ 
int a=10 ,b=25; 
void swap(int *,int *); 
Cout<<“before function calling”<<a<<b; 
swap(&a,&b); 
Cout<<“after function calling”<<a<<b; 
getch(); 
}
void swap(int *x,int *y) 
{ 
int z; 
z=*x; 
*x=*y; 
*y=z; 
Cout<<“value is”<<*x<<*y; 
}
Output: 
before function calling a= 10 b= 25 
value of x=25 and y=10 
after function calling a=25 b= 10
Using Reference Variables 
with Functions 
• To create a second name for a variable in a 
program, you can generate an alias, or an 
alternate name 
• In C++ a variable that acts as an alias for another 
variable is called a reference variable, or simply a 
reference
Declaring Reference Variables 
• You declare a reference variable by placing a type and an 
ampersand in front of a variable name, as in double 
&cash; and assigning another variable of the same type to 
the reference variable 
double someMoney; 
double &cash = someMoney; 
• A reference variable refers to the same memory address as 
does a variable, and a pointer holds the memory address of a 
variable
Pass By Reference 
void main() 
{ 
int i=10; 
int &j=i; // j is a reference variable of I 
cout<<“value”<<i<<“t”<<j; 
j=20; 
cout<<“modified value”<<i<<“t”<<j; 
getch(); 
}
Output:- 
Value 10 10 
modified value 20 20
Declaring Reference Variables 
• There are two differences between reference variables 
and pointers: 
– Pointers are more flexible 
– Reference variables are easier to use 
• You assign a value to a pointer by inserting an ampersand 
in front of the name of the variable whose address you 
want to store in the pointer 
• It shows that when you want to use the value stored in the 
pointer, you must use the asterisk to dereference the 
pointer, or use the value to which it points, instead of the 
address it holds
Comparing Pointers and 
References in a Function Header
Passing Variable Addresses to Reference 
Variables 
• Reference variables are easier to use because you don’t need 
any extra punctuation to output their values 
• You declare a reference variable by placing an ampersand in 
front of the variable’s name 
• You assign a value to a reference variable by using another 
variable’s name 
• The advantage to using reference variables lies in creating 
them in function headers
Passing Arrays to Functions 
• An array name actually represents a memory address 
• Thus, an array name is a pointer 
• The subscript used to access an element of an array indicates 
how much to add to the starting address to locate a value 
• When you pass an array to a function, you are actually 
passing an address 
• Any changes made to the array within the function also 
affect the original array
Passing an Array to a Function
Inline Functions 
• Each time you call a function in a C++ program, the computer 
must do the following: 
– Remember where to return when the function eventually ends 
– Provide memory for the function’s variables 
– Provide memory for any value returned by the function 
– Pass control to the function 
– Pass control back to the calling program 
• This extra activity constitutes the overhead, or cost of doing 
business, involved in calling a function
Using an Inline Function
Inline Functions 
• An inline function is a small function with no calling 
overhead 
• A copy of the function statements is placed directly into the 
compiled calling program 
• The inline function appears prior to the main(), which calls it 
• Any inline function must precede any function that calls it, 
which eliminates the need for prototyping in the calling 
function
Inline Functions 
• When you compile a program, the code for the inline 
function is placed directly within the main() function 
• You should use an inline function only in the following 
situations: 
– When you want to group statements together so that you can use a 
function name 
– When the number of statements is small (one or two lines in the body 
of the function) 
– When the function is called on few occasions
Using Default Arguments 
• When you don’t provide enough arguments in a function call, you 
usually want the compiler to issue a warning message for this error 
• Sometimes it is useful to create a function that supplies a default 
value for any missing parameters
Using Default Arguments 
• Two rules apply to default parameters: 
– If you assign a default value to any variable in a function prototype’s 
parameter list, then all parameters to the right of that variable also must have 
default values 
– If you omit any argument when you call a function that has default 
parameters, then you also must leave out all arguments to the right of that 
argument
Examples of Legal and Illegal 
Use of Functions with Default 
Parameters
Recursion 
• When function call itself repeatedly ,until some 
specified condition is met then this process is called 
recursion. 
• It is useful for writing repetitive problems where 
each action is stated in terms of previous result. 
• The need of recursion arises if logic of the problem 
is such that the solution of the problem depends 
upon the repetition of certain set of statements 
with different input values an with a condition.
Two basic requirements for recursion : 
1. The function must call itself again and again. 
2. It must have an exit condition.
Factorial using recursion 
void main() 
{ 
clrscr(); 
int rect(int); 
int n,fact; 
Cout<<“enter the no.”; 
cin>>n; 
fact=rect(n); 
Cout<<“factorial is”<<fact; 
getch();
int rect(int a) 
{ 
int b; 
if(a==1) 
{ 
return(1); 
} 
else 
{ 
b=a*rect(a-1); 
return(b); 
}
Stack representation 
. 3*2*Rec(1) 
3*Rec(2) 
Rec(3) Rec(3) 
3*Rec(2) 
Rec(3) 
. 
3*2*1 
3*2*Rec(1) 
3*Rec(2) 
Rec(3)
Advantages of recursion 
1. It make program code compact which is easier to 
write and understand. 
2. It is used with the data structures such as 
linklist,stack,queues etc. 
3. It is useful if a solution to a problem is in repetitive 
form. 
4. The compact code in a recursion simplifies the 
compilation as less number of lines need to be 
compiled.
Disadvantages 
1. Consume more storage space as recursion calls 
and automatic variables are stored in a stack. 
2. It is less efficient in comparison to normal program 
in case of speed and execution time 
3. Special care need to be taken for stopping 
condition in a recursion function 
4. If the recursion calls are not checked ,the 
computer may run out of memory.
Pointer to Function 
• Function pointers are pointers, i.e. variables, which 
point to the address of a function. 
• The syntax to declare to declare a function pointer 
is as follows: 
[return_type] (*pointer_name) 
[(list_of_parameters)] 
• example: 
Function declaration int add(int ,int); 
Pointer declaration int(*ptr)(int,int); 
Assigning address of the function to pointer 
ptr=&add;
Example 
#include<iostream.h> 
#include<conio.h> 
Void main() 
{ 
clrscr(); 
int x=3,y=5,z; 
int add(int , int); 
int (*ptr)(int,int); 
ptr=add; 
z=(*ptr)(x,y); 
Cout<<“result is ”<<z;
Contd… 
int add(int x,int y) 
{ 
return(x+y); 
}
FUNCTION 
OVERLOADING
Polymorphism 
• The word polymorphism is derived from Greek 
word Poly which means many and morphos which 
means forms. 
• Polymorphism can be defined as the ability to use 
the same name for two or more related but 
technically different tasks.
Overloading in C++ 
What is overloading 
– Overloading means assigning multiple 
meanings to a function name or operator 
symbol 
– It allows multiple definitions of a function with 
the same name, but different signatures. 
C++ supports 
– Function overloading 
– Operator overloading
Why is Overloading Useful? 
 Function overloading allows functions that 
conceptually perform the same task on 
objects of different types to be given the 
same name. 
 Operator overloading provides a convenient 
notation for manipulating user-defined 
objects with conventional operators.
Function Overloading 
• Is the process of using the same name for two or 
more functions 
• Requires each redefinition of a function to use a 
different function signature that is: 
– different types of parameters, 
– or sequence of parameters, 
– or number of parameters 
• Is used so that a programmer does not have to 
remember multiple function names
Function Overloading 
• Two or more functions can have the same name 
but different parameters 
• Example: 
int max(int a, int b) 
{ 
if (a>= b) 
return a; 
else 
return b; 
} 
float max(float a, float b) 
{ 
if (a>= b) 
return a; 
else 
return b; 
}
Overloading Function Call Resolution 
 Overloaded function call resolution is done by 
compiler during compilation 
– The function signature determines which definition 
is used 
 a Function signature consists of: 
– Parameter types and number of parameters 
supplied to a function 
 a Function return type is not part of function 
signature 
and is not used in function call resolution
Example 
Void sum(int,int); 
Void sum(double,double); 
Void sum(char,char); 
Void main() 
{ 
int a=10,b=20 ; 
double c=7.52,d=8.14; 
char e=‘a’ , f=‘b’ ; 
sum(a,b); 
sum(c,d);
Contd.. 
Void sum(int x,int y) 
{ 
Cout<<“n sum of integers are”<<x+y; 
} 
Void sum(double x,double y) 
{ 
Cout<<“n sum of two floating no are”<<x+y; 
} 
Void sum(char x,char y) 
{
• Output: 
Sum of integers 30 
sum of two floating no are 15.66 
sum of characters are 195
16717 functions in C++

More Related Content

What's hot (20)

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
functions of C++
functions of C++functions of C++
functions of C++
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Templates
TemplatesTemplates
Templates
 

Viewers also liked

Function in c++
Function in c++Function in c++
Function in c++Kumar
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Functions
FunctionsFunctions
FunctionsOnline
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Informatika 2 god_stucen
Informatika 2 god_stucenInformatika 2 god_stucen
Informatika 2 god_stucenZivko Boskov
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to classHaresh Jaiswal
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementsecomputernotes
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating dataecomputernotes
 
e computer notes - Reference variables
e computer notes - Reference variablese computer notes - Reference variables
e computer notes - Reference variablesecomputernotes
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Vocabulary: Ana & Paloma
Vocabulary: Ana & PalomaVocabulary: Ana & Paloma
Vocabulary: Ana & Palomaanapaloma94
 
Project: Ana & Paloma
Project: Ana & PalomaProject: Ana & Paloma
Project: Ana & Palomaanapaloma94
 

Viewers also liked (20)

C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Function in c++
Function in c++Function in c++
Function in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Functions
FunctionsFunctions
Functions
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
C++ funkcije
C++ funkcijeC++ funkcije
C++ funkcije
 
Informatika 2 god_stucen
Informatika 2 god_stucenInformatika 2 god_stucen
Informatika 2 god_stucen
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
 
Call by value
Call by valueCall by value
Call by value
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
e computer notes - Reference variables
e computer notes - Reference variablese computer notes - Reference variables
e computer notes - Reference variables
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Vocabulary: Ana & Paloma
Vocabulary: Ana & PalomaVocabulary: Ana & Paloma
Vocabulary: Ana & Paloma
 
Project: Ana & Paloma
Project: Ana & PalomaProject: Ana & Paloma
Project: Ana & Paloma
 

Similar to 16717 functions in C++ (20)

4th unit full
4th unit full4th unit full
4th unit full
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Functions
FunctionsFunctions
Functions
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Function
Function Function
Function
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 
Function C++
Function C++ Function C++
Function C++
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 

Recently uploaded

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 

Recently uploaded (20)

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 

16717 functions in C++

  • 2. What is function???? • Function is a self contained block of statements that perform a coherent task of some kind. • Every C program can be a thought of the collection of functions. • main( ) is also a function.
  • 3. Types of Functions. • Library functions – These are the in- -built functions of ‘C ’library. – These are already defined in header files. – e.g. printf( ); is a function which is used to print at output. It is defined in ‘stdio.h ’ file . • User defined functions. – Programmer can create their own function in C to perform specific task
  • 4. Why use functions? • Writing functions avoids rewriting of the same code again and again in the program. • Using function large programs can be reduced to smaller ones. It is easy to debug and find out the errors in it. • Using a function it becomes easier to write program to keep track of what they are doing.
  • 5. Function Declaration ret_type func_name(data_type par1,data_type par2); Function Defination ret_type func_name(data_type par1,data_type par2) {} Function Call func_name(data_type par1,data_type par2);
  • 6. Function prototype • A prototype statement helps the compiler to check the return type and arguments type of the function. • A prototype function consist of the functions return type, name and argument list. • Example – int sum( int x, int y);
  • 7. Example #include<iostream.h> #include<conio.h> void main() { clrscr(); void print(); /*func declaration print(); /*func calling printf(“no parameter and no return value”); print(); getch(); } void print() /*func defination { For(int i=1;i<=30;i++) { Cout<<“*”; } Cout<<endl; }
  • 8. #include<conio.h> void main() { clrscr(); int a=10,b=20; int sum(int,int); int c=sum(a,b); /*actual arguments Cout<<“sum is” << c; getch(); } intsum(int x, int y) /*formal arguments { int s; s=x+y; return(s); /*return value }
  • 9. Function parameters • The parameters are local variables inside the body of the function. – When the function is called they will have the values passed in. – The function gets a copy of the values passed in (we will later see how to pass a reference to a variable).
  • 10. Arguments/Parameters • one-to-one correspondence between the arguments in a function call and the parameters in the function definition. int argument1; double argument2; // function call (in another function, such as main) result = thefunctionname(argument1, argument2); // function definition int thefunctionname(int parameter1, double parameter2){ // Now the function can use the two parameters // parameter1 = argument 1, parameter2 = argument2
  • 11. a) Actual arguments:-the arguments of calling function are actual arguments. b) Formal arguments:-arguments of called function are formal arguments. c) Argument list:-means variable name enclosed within the paranthesis.they must be separated by comma d) Return value:-it is the outcome of the function. the result obtained by the function is sent back to the calling function through the return statement.
  • 12. Return statement • It is used to return the value to the calling function. • It can be used in fallowing ways a) return(expression) return(a+b); b) A function may use one or more return statement depending upon condition if(a>b) return(a); else return(b);
  • 13. c) return(&a); returns the address of the variable d)returns(*p);returns value of variable through the pointer e)return(sqrt(r)); f)return(float (sqrt(2.4)));
  • 14. Categories of functions • A function with no parameter and no return value • A function with parameter and no return value • A function with parameter and return value • A function without parameter and return value
  • 15. A function with no parameter and no return value #include<conio.h> void main() { clrscr(); void print(); /*func declaration print(); /*func calling Cout<<“no parameter and no return value”; print(); getch(); } void print() /*func defination { For(int i=1;i<=30;i++) { cout<<“*”; } Cout<<“n”; }
  • 16. A function with no parameter and no return value • There is no data transfer between calling and called function • The function is only executed and nothing is obtained • Such functions may be used to print some messages, draw stars etc
  • 17. A function with parameter and no return value #include<conio.h> void main() { clrscr(); int a=10,b=20; void mul(int,int); mul(a,b); /*actual arguments getch(); } void mul(int x, int y) /*formal arguments { int s; s=x*y; Cout<<“mul is” << s; }
  • 18. A function with parameter and return value #include<conio.h> void main() { clrscr(); int a=10,b=20,c; int max(int,int); c=max(a,b); Cout<<“greatest no is” <<c; getch(); } int max(int x, int y) { if(x>y) return(x); else
  • 19. A function without parameter and return value #include<conio.h> void main() { clrscr(); int a=10,b=20; int sum(); int c=sum(); /*actual arguments Cout<<“sum is”<< c; getch(); } int sum() /*formal arguments { int x=10,y=30; return(x+y); /*return value }
  • 20. Argument passing techniques • Pass By Value • Pass By Reference • Pass By Pointeraddress
  • 21. Call By Value • It is a default mechanism for argument passing. • When an argument is passed by value then the copy of argument is made know as formal arguments which is stored at separate memory location • Any changes made in the formal argument are not reflected back to actual argument, rather they remain local to the block which are lost once the control is returned back to calling program
  • 22. Example void main() { int a=10,b=20; void swap(int,int); Cout<<“before function calling”<<a<<b; swap(a,b); Cout<<“after function calling”<<a<<b; getch(); }
  • 23. void swap(int x,int y) { int z; z=x; x=y; y=z; Cout<<“value is”<<x<<y; }
  • 24. Output: before function calling a=10 b=20 value of x=20 and y= 10 after function calling a=10 b=20
  • 25. Call By pointer/address • In this instead of passing value, address are passed. • Here formal arguments are pointers to the actual arguments • Hence change made in the argument are permanent.
  • 26. Example Void main() { int a=10 ,b=25; void swap(int *,int *); Cout<<“before function calling”<<a<<b; swap(&a,&b); Cout<<“after function calling”<<a<<b; getch(); }
  • 27. void swap(int *x,int *y) { int z; z=*x; *x=*y; *y=z; Cout<<“value is”<<*x<<*y; }
  • 28. Output: before function calling a= 10 b= 25 value of x=25 and y=10 after function calling a=25 b= 10
  • 29. Using Reference Variables with Functions • To create a second name for a variable in a program, you can generate an alias, or an alternate name • In C++ a variable that acts as an alias for another variable is called a reference variable, or simply a reference
  • 30. Declaring Reference Variables • You declare a reference variable by placing a type and an ampersand in front of a variable name, as in double &cash; and assigning another variable of the same type to the reference variable double someMoney; double &cash = someMoney; • A reference variable refers to the same memory address as does a variable, and a pointer holds the memory address of a variable
  • 31. Pass By Reference void main() { int i=10; int &j=i; // j is a reference variable of I cout<<“value”<<i<<“t”<<j; j=20; cout<<“modified value”<<i<<“t”<<j; getch(); }
  • 32. Output:- Value 10 10 modified value 20 20
  • 33. Declaring Reference Variables • There are two differences between reference variables and pointers: – Pointers are more flexible – Reference variables are easier to use • You assign a value to a pointer by inserting an ampersand in front of the name of the variable whose address you want to store in the pointer • It shows that when you want to use the value stored in the pointer, you must use the asterisk to dereference the pointer, or use the value to which it points, instead of the address it holds
  • 34. Comparing Pointers and References in a Function Header
  • 35. Passing Variable Addresses to Reference Variables • Reference variables are easier to use because you don’t need any extra punctuation to output their values • You declare a reference variable by placing an ampersand in front of the variable’s name • You assign a value to a reference variable by using another variable’s name • The advantage to using reference variables lies in creating them in function headers
  • 36. Passing Arrays to Functions • An array name actually represents a memory address • Thus, an array name is a pointer • The subscript used to access an element of an array indicates how much to add to the starting address to locate a value • When you pass an array to a function, you are actually passing an address • Any changes made to the array within the function also affect the original array
  • 37. Passing an Array to a Function
  • 38. Inline Functions • Each time you call a function in a C++ program, the computer must do the following: – Remember where to return when the function eventually ends – Provide memory for the function’s variables – Provide memory for any value returned by the function – Pass control to the function – Pass control back to the calling program • This extra activity constitutes the overhead, or cost of doing business, involved in calling a function
  • 39. Using an Inline Function
  • 40. Inline Functions • An inline function is a small function with no calling overhead • A copy of the function statements is placed directly into the compiled calling program • The inline function appears prior to the main(), which calls it • Any inline function must precede any function that calls it, which eliminates the need for prototyping in the calling function
  • 41. Inline Functions • When you compile a program, the code for the inline function is placed directly within the main() function • You should use an inline function only in the following situations: – When you want to group statements together so that you can use a function name – When the number of statements is small (one or two lines in the body of the function) – When the function is called on few occasions
  • 42. Using Default Arguments • When you don’t provide enough arguments in a function call, you usually want the compiler to issue a warning message for this error • Sometimes it is useful to create a function that supplies a default value for any missing parameters
  • 43. Using Default Arguments • Two rules apply to default parameters: – If you assign a default value to any variable in a function prototype’s parameter list, then all parameters to the right of that variable also must have default values – If you omit any argument when you call a function that has default parameters, then you also must leave out all arguments to the right of that argument
  • 44. Examples of Legal and Illegal Use of Functions with Default Parameters
  • 45. Recursion • When function call itself repeatedly ,until some specified condition is met then this process is called recursion. • It is useful for writing repetitive problems where each action is stated in terms of previous result. • The need of recursion arises if logic of the problem is such that the solution of the problem depends upon the repetition of certain set of statements with different input values an with a condition.
  • 46. Two basic requirements for recursion : 1. The function must call itself again and again. 2. It must have an exit condition.
  • 47. Factorial using recursion void main() { clrscr(); int rect(int); int n,fact; Cout<<“enter the no.”; cin>>n; fact=rect(n); Cout<<“factorial is”<<fact; getch();
  • 48. int rect(int a) { int b; if(a==1) { return(1); } else { b=a*rect(a-1); return(b); }
  • 49. Stack representation . 3*2*Rec(1) 3*Rec(2) Rec(3) Rec(3) 3*Rec(2) Rec(3) . 3*2*1 3*2*Rec(1) 3*Rec(2) Rec(3)
  • 50. Advantages of recursion 1. It make program code compact which is easier to write and understand. 2. It is used with the data structures such as linklist,stack,queues etc. 3. It is useful if a solution to a problem is in repetitive form. 4. The compact code in a recursion simplifies the compilation as less number of lines need to be compiled.
  • 51. Disadvantages 1. Consume more storage space as recursion calls and automatic variables are stored in a stack. 2. It is less efficient in comparison to normal program in case of speed and execution time 3. Special care need to be taken for stopping condition in a recursion function 4. If the recursion calls are not checked ,the computer may run out of memory.
  • 52. Pointer to Function • Function pointers are pointers, i.e. variables, which point to the address of a function. • The syntax to declare to declare a function pointer is as follows: [return_type] (*pointer_name) [(list_of_parameters)] • example: Function declaration int add(int ,int); Pointer declaration int(*ptr)(int,int); Assigning address of the function to pointer ptr=&add;
  • 53. Example #include<iostream.h> #include<conio.h> Void main() { clrscr(); int x=3,y=5,z; int add(int , int); int (*ptr)(int,int); ptr=add; z=(*ptr)(x,y); Cout<<“result is ”<<z;
  • 54. Contd… int add(int x,int y) { return(x+y); }
  • 56. Polymorphism • The word polymorphism is derived from Greek word Poly which means many and morphos which means forms. • Polymorphism can be defined as the ability to use the same name for two or more related but technically different tasks.
  • 57. Overloading in C++ What is overloading – Overloading means assigning multiple meanings to a function name or operator symbol – It allows multiple definitions of a function with the same name, but different signatures. C++ supports – Function overloading – Operator overloading
  • 58. Why is Overloading Useful?  Function overloading allows functions that conceptually perform the same task on objects of different types to be given the same name.  Operator overloading provides a convenient notation for manipulating user-defined objects with conventional operators.
  • 59. Function Overloading • Is the process of using the same name for two or more functions • Requires each redefinition of a function to use a different function signature that is: – different types of parameters, – or sequence of parameters, – or number of parameters • Is used so that a programmer does not have to remember multiple function names
  • 60.
  • 61. Function Overloading • Two or more functions can have the same name but different parameters • Example: int max(int a, int b) { if (a>= b) return a; else return b; } float max(float a, float b) { if (a>= b) return a; else return b; }
  • 62. Overloading Function Call Resolution  Overloaded function call resolution is done by compiler during compilation – The function signature determines which definition is used  a Function signature consists of: – Parameter types and number of parameters supplied to a function  a Function return type is not part of function signature and is not used in function call resolution
  • 63. Example Void sum(int,int); Void sum(double,double); Void sum(char,char); Void main() { int a=10,b=20 ; double c=7.52,d=8.14; char e=‘a’ , f=‘b’ ; sum(a,b); sum(c,d);
  • 64. Contd.. Void sum(int x,int y) { Cout<<“n sum of integers are”<<x+y; } Void sum(double x,double y) { Cout<<“n sum of two floating no are”<<x+y; } Void sum(char x,char y) {
  • 65. • Output: Sum of integers 30 sum of two floating no are 15.66 sum of characters are 195