SlideShare a Scribd company logo
C++ PRESENTATION
FUNCTIONS
P.P.Krishnaraj
RSET
FUNCTIONS
Void main()
{
Statement 1;
Statement 2;
Statement 3;
.
.
.
.
Statement n;
}
Void main()
{
Statement 1;
Statement 2;
Sum1();
Statement 3;
Statement 4;
Sum2();
Statement 5;
Statement 6;
}
P.P.Krishnaraj RSET
Advantages
• Support for modular programming
• Reduction in program size.
• Code duplication is avoided.
• Code reusability is provided.
• Functions can be called repetitively.
• A set of functions can be used to form libraries.
P.P.Krishnaraj RSET
Types
1.Built in functions :-
are part of compiler package.
Part of standard library made available by compiler.
Can be used in any program by including respective header file.
2. User defined functions:-
Created by user or programmer.
Created as per requirement of the program.
P.P.Krishnaraj RSET
User defined function
Void main()
{
Statement 1;
Statement 2;
multiply();
Statement3;
-------------;
-------------;
Sum();
return0;
}
multiply()
{
-----;
} P.P.Krishnaraj RSET
Parts of a function
main function
{
function prototype declaration
function call
-------;
}
function declaratory/definition
{
------;
return statement
}
P.P.Krishnaraj RSET
Function prototype
A function prototype is a declaration of a function that tells the program about
the type of value returned by the function, name of function, number and type
of arguments.
4 parts
i. Return type
ii. Function name
iii. Argument list
iv. Terminating semicolon
Variable declaration
Data_type variable_name ;
int x=5;
float marks;
int price;
Syntax: Return_type function_name (parameter list/argument);
int add(int,int);
void add(void);
int add(float,int);
P.P.Krishnaraj RSET
Function call
A function must be called by its name followed by argument list enclosed
in semicolon.
Suppose int add(int,int); //prototype
Now to this function add(x,y); //function call
or
add(40,60);
Syntax: function_name (parameter list/argument);
add(x,y);
add(40,60);
add(void); or add();
Note: data type not to be mentioned.
P.P.Krishnaraj RSET
Suppose int add(int,float); //prototype
add(x,y); //function call
int float
Now suppose the function return some integer value, you can use a
variable to store that value.
i.e z=add(x,y);
P.P.Krishnaraj RSET
Parts of a function
main function
{
function prototype declaration
function call
}
function declaratory/definition
{
return statement
}
P.P.Krishnaraj RSET
Function definition
Syntax: function_type function_name(parameter list)
{
Local variable declaration;
Function body statement;
Return statement;
}
Function
header
Function
body
2 parts
Note: no semicolon in header
Example: int add(int,int); //prototype
Z=add(x,y); //function call
int add(int a,int b) //function definition
{
body statement;
Return(a+b);
}
P.P.Krishnaraj RSET
#include<iostream.h>
using namespace std;
int main()
{
return 0;
}
void print()
{
cout<<“2 is even no.”<<endl;
}
Print();
Error message since
print was not declared in
the scope.
int main()
{
Print();
return 0;
}
void print()
{
cout<<“2 is even no.”<<endl;
}
#include<iostream.h>
using namespace std;
P.P.Krishnaraj RSET
Why prototyping?????
#include<iostream.h>
using namespace std;
int main()
{
print();
return 0;
}
void print()
{
cout<<“2 is even no.”<<endl;
}
void print();
function_name(parameter list/argument);
Return_type function_name (parameter list/argument);
function_type function_name(parameter list)
P.P.Krishnaraj RSET
Parts of a function
main function
{
function prototype declaration
function call
-------;
}
function declaratory/definition
{
------;
return statement
}
Return_type function_name(arguments); eg: int add(int);
function_name(actual arguments); eg: add(a);
Return_type function_name(formal arguments) eg: int add(int X);
P.P.Krishnaraj RSET
Function categories
i) Function with no return value and no argument.
void add(void);
ii) Function with arguments passed and no return value.
void add(int,int);
iii) Function with no arguments but returns a value.
int add(void);
iv) Function with arguments and returns a value.
int add(int,int);
P.P.Krishnaraj RSET
I. Function with no return value and no argument
void main()
{
void disp(void); //prototype
disp(); //caller function
return 0;
}
void disp() //calle function
{
cout<<“--------”<<endl;
}
No arguments passed
from caller to calle
No value returned from
calle to caller function
P.P.Krishnaraj RSET
//program to print square of a number using functions.
void main()
{
void sqr(void);
sqr();
getch();
return 0;
}
void sqr()
{
int no;
cout<<“enter a no.”;
cin>>no;
cout<<“square of”<<no<<“is”<<no*no;
}
P.P.Krishnaraj RSET
ii. Function will not return any value but passes argument
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
getch();
return 0;
}
void add(int x,int y)
{
int c;
c=x+y;
cout<<“addition is”<<c;
}
add(a,b);
a b
void add(int x,int y);
P.P.Krishnaraj RSET
iii) Function with arguments and return value
main function
{
int sqr(int); //function prototype
int a,ans;
cout<<“enter a number”;
cin>>a;
ans=sqr(a); //function call
cout<<“square of number is”<<ans;
getch();
return 0;
}
int sqr(int X) //function declaratory/definition
{
return(X*X);
}
P.P.Krishnaraj RSET
iv) Function with no arguments but returns a value
int main()
{
int add(void);
int z;
z=add();
cout<<sum of 2 nos is”<<z;
getch();
return 0;
}
int add(void);
{
int a,b;
cout<<“enter 2 nos”;
cin>>a>>b;
return(a+b);
}
Function call
add(x,y);
i.e z=add(x,y);
P.P.Krishnaraj RSET
Pointers
• Special type of variables which hold the address of another variable
i.e no values or datas are stored but it points to another variable
where data is stored.
100
int a=100;
1)a name
 2)value
3)address
Pointer stores
this memory
location, no
direct value is
stored.
P.P.Krishnaraj RSET
Declaration: Data_type *variable_name;
int a=100;
int *ptr;
Intialisation: ptr=&a; //referencing
“Address of” operator
100
address 1
a
address 1
ptr
address 2
or
int a=100;
int *ptr=&a;
Pointer initialisation
and declaration
P.P.Krishnaraj RSET
Note: a pointer can be used to point to another pointer also i.e it can
store the address of another pointer.
int a=100;
int *ptr=&a;
int **ptr1=&ptr;
100
address 1
a
address 1
ptr
address 2
address 2
ptr1
address 3
Note: pointer 1 points to address of ptr.
cout<<a; //100
cout<<*ptr;//100
cout<< **ptr1; //100
P.P.Krishnaraj RSET
#include<iosream.h>
#include<conio.h>
int main()
{
int a=100;
int *p1;
int * *p2;
p1=&a; //p1 points to address of a
p2=&p1; // p2 points to address of p1
cout<<“address of a”<<&a;
cout<<“address of a”<<p1;
cout<<“value of a”<< *p1;
cout<<“value of a”<< * *p2;
cout<<p2;
cout<< *p2;
getch();
}
100
address 1
a
address 2
p2
address 3
address 1
p1
address 2
Chain of pointers
P.P.Krishnaraj RSET
Reference variable in C++
When a variable is declared as reference, it becomes an alternative name
for an existing variable. A variable can be declared as reference by
putting ‘&’ in the declaration.
int a=100;
Now we will use a reference variable i.e two names
for same memory location.
int a=100;
int &ref=a; //initialization and declaration
Now in program we can use either “a” or an
alternative name “ref”
C=a+b; //same output
C=ref+b;
100
a
0012dcD2
P.P.Krishnaraj RSET
Program using reference variable
#include<iostream.h>
#include<conio.h>
int main()
{
int a=100;
int &ref=a;
cout<<“value of a is”<<a;
cout<<“value of ref is”<<ref;
cout<<address of a is”<<&a;
cout<<address of a is”<<&ref;
getch();
}
100
100
0012dcD2
0012dcD2
Both “a” and “ref” are used for
same memory location as
alternative name
P.P.Krishnaraj RSET
Call by value
A function can be invoked in two manners
(i)call by value
(ii)call by reference
The call by value method copies the value of actual parameters into formal
parameters i.e the function creates its own copy of arguments and uses
them.
add(a,b);
}
void add(int x,int y);
{
--------;
}
Values of variables a
and b are passed to
X,Y
Now if you change
the value of X and Y,
those changes are
not seen in a and b
Call by value
where the values of
variable are passed
to functions
P.P.Krishnaraj RSET
/* program to illustrate the concept of call by value */
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
getch();
return 0;
}
void add(int x,int y)
{
int c;
c=x+y;
cout<<“addition is”<<c; P.P.Krishnaraj RSET
Call by reference
In call by reference method in place of calling a value to the function being
called , a reference to the original variable is passed .i.e the same variable value
can be accessed by any of the two names.
add(a,b);
}
void add(int &x,int &y);
{
--------;
}
In function call
We write reference
variable for formal
arguments
&X,&Y will be the
reference variable for
a and b. if we change
X and Y, Value of a
and b are changed
accordingly
No need for return
statement
P.P.Krishnaraj RSET
Program to illustrate call by reference
#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
int main()
{
int a,b;
cout<<“enter the values of a and b”;
cin>>a>>b;
cout<<“before swaping”;
cout<<“A”<<a;
cout<<“B”<<b;
swap(a,b);
cout<<“after swaping”;
cout<<“A”<<a;
cout<<“B”<<b;
getch();
}
void swap(int &X,&Y)
{
int temp;
temp=X;
X=Y;
Y=X;
}
P.P.Krishnaraj RSET
Ad

More Related Content

Similar to concept of functions and its types in c++ language (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
C# 7.0, 7.1, 7.2
C# 7.0, 7.1, 7.2C# 7.0, 7.1, 7.2
C# 7.0, 7.1, 7.2
Miguel Angel Teheran Garcia
 
Programming Fundamentals lecture-10.pptx
Programming Fundamentals lecture-10.pptxProgramming Fundamentals lecture-10.pptx
Programming Fundamentals lecture-10.pptx
singyali199
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
RehmanRasheed3
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Dr. Chandrakant Divate
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
temkin abdlkader
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
C programming is a powerful, general-purpose language used for developing ope...
C programming is a powerful, general-purpose language used for developing ope...C programming is a powerful, general-purpose language used for developing ope...
C programming is a powerful, general-purpose language used for developing ope...
nadeemsk351
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
ankush_kumar
 
Introduction to functions in C programming language
Introduction to functions in C programming languageIntroduction to functions in C programming language
Introduction to functions in C programming language
ssuserbad56d
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
Nauman Rehman
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
 
Function in C program
Function in C programFunction in C program
Function in C program
Nurul Zakiah Zamri Tan
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
Programming Fundamentals lecture-10.pptx
Programming Fundamentals lecture-10.pptxProgramming Fundamentals lecture-10.pptx
Programming Fundamentals lecture-10.pptx
singyali199
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Dr. Chandrakant Divate
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
C programming is a powerful, general-purpose language used for developing ope...
C programming is a powerful, general-purpose language used for developing ope...C programming is a powerful, general-purpose language used for developing ope...
C programming is a powerful, general-purpose language used for developing ope...
nadeemsk351
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
ankush_kumar
 
Introduction to functions in C programming language
Introduction to functions in C programming languageIntroduction to functions in C programming language
Introduction to functions in C programming language
ssuserbad56d
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
Nauman Rehman
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
rebin5725
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 

More from soniasharmafdp (15)

hashing in data structure for engineering.pptx
hashing in data structure for engineering.pptxhashing in data structure for engineering.pptx
hashing in data structure for engineering.pptx
soniasharmafdp
 
unit 2- linked list- PPT for btech students.pdf
unit 2- linked list- PPT for btech students.pdfunit 2- linked list- PPT for btech students.pdf
unit 2- linked list- PPT for btech students.pdf
soniasharmafdp
 
graphs in data structure for Btech students.pdf
graphs in data structure for Btech students.pdfgraphs in data structure for Btech students.pdf
graphs in data structure for Btech students.pdf
soniasharmafdp
 
New one with new ppt Microsoft Office PowerPoint Presentation.pptx
New one with new ppt Microsoft Office PowerPoint Presentation.pptxNew one with new ppt Microsoft Office PowerPoint Presentation.pptx
New one with new ppt Microsoft Office PowerPoint Presentation.pptx
soniasharmafdp
 
hashing in data structure for Btech .pptx
hashing in data structure for Btech .pptxhashing in data structure for Btech .pptx
hashing in data structure for Btech .pptx
soniasharmafdp
 
classes data type for Btech students.ppt
classes data type for Btech students.pptclasses data type for Btech students.ppt
classes data type for Btech students.ppt
soniasharmafdp
 
hashing in data structure for Btech.pptx
hashing in data structure for Btech.pptxhashing in data structure for Btech.pptx
hashing in data structure for Btech.pptx
soniasharmafdp
 
labwork practice on inhetitance-1.pptx
labwork  practice on  inhetitance-1.pptxlabwork  practice on  inhetitance-1.pptx
labwork practice on inhetitance-1.pptx
soniasharmafdp
 
friend_derivedclasspresentation1234.pptx
friend_derivedclasspresentation1234.pptxfriend_derivedclasspresentation1234.pptx
friend_derivedclasspresentation1234.pptx
soniasharmafdp
 
OOPS PROGRAM for the clarity of functions in c++
OOPS PROGRAM for the clarity of functions in c++OOPS PROGRAM for the clarity of functions in c++
OOPS PROGRAM for the clarity of functions in c++
soniasharmafdp
 
functions in c++ basic concept for students
functions in c++ basic concept for studentsfunctions in c++ basic concept for students
functions in c++ basic concept for students
soniasharmafdp
 
Data Bill.pdf
Data Bill.pdfData Bill.pdf
Data Bill.pdf
soniasharmafdp
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
soniasharmafdp
 
Linked stack-and-linked-queue
Linked stack-and-linked-queueLinked stack-and-linked-queue
Linked stack-and-linked-queue
soniasharmafdp
 
Datastructureitstypes
DatastructureitstypesDatastructureitstypes
Datastructureitstypes
soniasharmafdp
 
hashing in data structure for engineering.pptx
hashing in data structure for engineering.pptxhashing in data structure for engineering.pptx
hashing in data structure for engineering.pptx
soniasharmafdp
 
unit 2- linked list- PPT for btech students.pdf
unit 2- linked list- PPT for btech students.pdfunit 2- linked list- PPT for btech students.pdf
unit 2- linked list- PPT for btech students.pdf
soniasharmafdp
 
graphs in data structure for Btech students.pdf
graphs in data structure for Btech students.pdfgraphs in data structure for Btech students.pdf
graphs in data structure for Btech students.pdf
soniasharmafdp
 
New one with new ppt Microsoft Office PowerPoint Presentation.pptx
New one with new ppt Microsoft Office PowerPoint Presentation.pptxNew one with new ppt Microsoft Office PowerPoint Presentation.pptx
New one with new ppt Microsoft Office PowerPoint Presentation.pptx
soniasharmafdp
 
hashing in data structure for Btech .pptx
hashing in data structure for Btech .pptxhashing in data structure for Btech .pptx
hashing in data structure for Btech .pptx
soniasharmafdp
 
classes data type for Btech students.ppt
classes data type for Btech students.pptclasses data type for Btech students.ppt
classes data type for Btech students.ppt
soniasharmafdp
 
hashing in data structure for Btech.pptx
hashing in data structure for Btech.pptxhashing in data structure for Btech.pptx
hashing in data structure for Btech.pptx
soniasharmafdp
 
labwork practice on inhetitance-1.pptx
labwork  practice on  inhetitance-1.pptxlabwork  practice on  inhetitance-1.pptx
labwork practice on inhetitance-1.pptx
soniasharmafdp
 
friend_derivedclasspresentation1234.pptx
friend_derivedclasspresentation1234.pptxfriend_derivedclasspresentation1234.pptx
friend_derivedclasspresentation1234.pptx
soniasharmafdp
 
OOPS PROGRAM for the clarity of functions in c++
OOPS PROGRAM for the clarity of functions in c++OOPS PROGRAM for the clarity of functions in c++
OOPS PROGRAM for the clarity of functions in c++
soniasharmafdp
 
functions in c++ basic concept for students
functions in c++ basic concept for studentsfunctions in c++ basic concept for students
functions in c++ basic concept for students
soniasharmafdp
 
1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf1.3- infix-ti-postfix.pdf
1.3- infix-ti-postfix.pdf
soniasharmafdp
 
Linked stack-and-linked-queue
Linked stack-and-linked-queueLinked stack-and-linked-queue
Linked stack-and-linked-queue
soniasharmafdp
 
Ad

Recently uploaded (20)

How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Optimizing Reinforced Concrete Cantilever Retaining Walls Using Gases Brownia...
Journal of Soft Computing in Civil Engineering
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Ad

concept of functions and its types in c++ language

  • 2. FUNCTIONS Void main() { Statement 1; Statement 2; Statement 3; . . . . Statement n; } Void main() { Statement 1; Statement 2; Sum1(); Statement 3; Statement 4; Sum2(); Statement 5; Statement 6; } P.P.Krishnaraj RSET
  • 3. Advantages • Support for modular programming • Reduction in program size. • Code duplication is avoided. • Code reusability is provided. • Functions can be called repetitively. • A set of functions can be used to form libraries. P.P.Krishnaraj RSET
  • 4. Types 1.Built in functions :- are part of compiler package. Part of standard library made available by compiler. Can be used in any program by including respective header file. 2. User defined functions:- Created by user or programmer. Created as per requirement of the program. P.P.Krishnaraj RSET
  • 5. User defined function Void main() { Statement 1; Statement 2; multiply(); Statement3; -------------; -------------; Sum(); return0; } multiply() { -----; } P.P.Krishnaraj RSET
  • 6. Parts of a function main function { function prototype declaration function call -------; } function declaratory/definition { ------; return statement } P.P.Krishnaraj RSET
  • 7. Function prototype A function prototype is a declaration of a function that tells the program about the type of value returned by the function, name of function, number and type of arguments. 4 parts i. Return type ii. Function name iii. Argument list iv. Terminating semicolon Variable declaration Data_type variable_name ; int x=5; float marks; int price; Syntax: Return_type function_name (parameter list/argument); int add(int,int); void add(void); int add(float,int); P.P.Krishnaraj RSET
  • 8. Function call A function must be called by its name followed by argument list enclosed in semicolon. Suppose int add(int,int); //prototype Now to this function add(x,y); //function call or add(40,60); Syntax: function_name (parameter list/argument); add(x,y); add(40,60); add(void); or add(); Note: data type not to be mentioned. P.P.Krishnaraj RSET
  • 9. Suppose int add(int,float); //prototype add(x,y); //function call int float Now suppose the function return some integer value, you can use a variable to store that value. i.e z=add(x,y); P.P.Krishnaraj RSET
  • 10. Parts of a function main function { function prototype declaration function call } function declaratory/definition { return statement } P.P.Krishnaraj RSET
  • 11. Function definition Syntax: function_type function_name(parameter list) { Local variable declaration; Function body statement; Return statement; } Function header Function body 2 parts Note: no semicolon in header Example: int add(int,int); //prototype Z=add(x,y); //function call int add(int a,int b) //function definition { body statement; Return(a+b); } P.P.Krishnaraj RSET
  • 12. #include<iostream.h> using namespace std; int main() { return 0; } void print() { cout<<“2 is even no.”<<endl; } Print(); Error message since print was not declared in the scope. int main() { Print(); return 0; } void print() { cout<<“2 is even no.”<<endl; } #include<iostream.h> using namespace std; P.P.Krishnaraj RSET
  • 13. Why prototyping????? #include<iostream.h> using namespace std; int main() { print(); return 0; } void print() { cout<<“2 is even no.”<<endl; } void print(); function_name(parameter list/argument); Return_type function_name (parameter list/argument); function_type function_name(parameter list) P.P.Krishnaraj RSET
  • 14. Parts of a function main function { function prototype declaration function call -------; } function declaratory/definition { ------; return statement } Return_type function_name(arguments); eg: int add(int); function_name(actual arguments); eg: add(a); Return_type function_name(formal arguments) eg: int add(int X); P.P.Krishnaraj RSET
  • 15. Function categories i) Function with no return value and no argument. void add(void); ii) Function with arguments passed and no return value. void add(int,int); iii) Function with no arguments but returns a value. int add(void); iv) Function with arguments and returns a value. int add(int,int); P.P.Krishnaraj RSET
  • 16. I. Function with no return value and no argument void main() { void disp(void); //prototype disp(); //caller function return 0; } void disp() //calle function { cout<<“--------”<<endl; } No arguments passed from caller to calle No value returned from calle to caller function P.P.Krishnaraj RSET
  • 17. //program to print square of a number using functions. void main() { void sqr(void); sqr(); getch(); return 0; } void sqr() { int no; cout<<“enter a no.”; cin>>no; cout<<“square of”<<no<<“is”<<no*no; } P.P.Krishnaraj RSET
  • 18. ii. Function will not return any value but passes argument #include<iostream.h> #include<conio.h> void add(int,int); int main() { int a,b; cout<<“enter values of a and b”<<endl; cin>>a>>b; add(a,b); getch(); return 0; } void add(int x,int y) { int c; c=x+y; cout<<“addition is”<<c; } add(a,b); a b void add(int x,int y); P.P.Krishnaraj RSET
  • 19. iii) Function with arguments and return value main function { int sqr(int); //function prototype int a,ans; cout<<“enter a number”; cin>>a; ans=sqr(a); //function call cout<<“square of number is”<<ans; getch(); return 0; } int sqr(int X) //function declaratory/definition { return(X*X); } P.P.Krishnaraj RSET
  • 20. iv) Function with no arguments but returns a value int main() { int add(void); int z; z=add(); cout<<sum of 2 nos is”<<z; getch(); return 0; } int add(void); { int a,b; cout<<“enter 2 nos”; cin>>a>>b; return(a+b); } Function call add(x,y); i.e z=add(x,y); P.P.Krishnaraj RSET
  • 21. Pointers • Special type of variables which hold the address of another variable i.e no values or datas are stored but it points to another variable where data is stored. 100 int a=100; 1)a name  2)value 3)address Pointer stores this memory location, no direct value is stored. P.P.Krishnaraj RSET
  • 22. Declaration: Data_type *variable_name; int a=100; int *ptr; Intialisation: ptr=&a; //referencing “Address of” operator 100 address 1 a address 1 ptr address 2 or int a=100; int *ptr=&a; Pointer initialisation and declaration P.P.Krishnaraj RSET
  • 23. Note: a pointer can be used to point to another pointer also i.e it can store the address of another pointer. int a=100; int *ptr=&a; int **ptr1=&ptr; 100 address 1 a address 1 ptr address 2 address 2 ptr1 address 3 Note: pointer 1 points to address of ptr. cout<<a; //100 cout<<*ptr;//100 cout<< **ptr1; //100 P.P.Krishnaraj RSET
  • 24. #include<iosream.h> #include<conio.h> int main() { int a=100; int *p1; int * *p2; p1=&a; //p1 points to address of a p2=&p1; // p2 points to address of p1 cout<<“address of a”<<&a; cout<<“address of a”<<p1; cout<<“value of a”<< *p1; cout<<“value of a”<< * *p2; cout<<p2; cout<< *p2; getch(); } 100 address 1 a address 2 p2 address 3 address 1 p1 address 2 Chain of pointers P.P.Krishnaraj RSET
  • 25. Reference variable in C++ When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. int a=100; Now we will use a reference variable i.e two names for same memory location. int a=100; int &ref=a; //initialization and declaration Now in program we can use either “a” or an alternative name “ref” C=a+b; //same output C=ref+b; 100 a 0012dcD2 P.P.Krishnaraj RSET
  • 26. Program using reference variable #include<iostream.h> #include<conio.h> int main() { int a=100; int &ref=a; cout<<“value of a is”<<a; cout<<“value of ref is”<<ref; cout<<address of a is”<<&a; cout<<address of a is”<<&ref; getch(); } 100 100 0012dcD2 0012dcD2 Both “a” and “ref” are used for same memory location as alternative name P.P.Krishnaraj RSET
  • 27. Call by value A function can be invoked in two manners (i)call by value (ii)call by reference The call by value method copies the value of actual parameters into formal parameters i.e the function creates its own copy of arguments and uses them. add(a,b); } void add(int x,int y); { --------; } Values of variables a and b are passed to X,Y Now if you change the value of X and Y, those changes are not seen in a and b Call by value where the values of variable are passed to functions P.P.Krishnaraj RSET
  • 28. /* program to illustrate the concept of call by value */ #include<iostream.h> #include<conio.h> void add(int,int); int main() { int a,b; cout<<“enter values of a and b”<<endl; cin>>a>>b; add(a,b); getch(); return 0; } void add(int x,int y) { int c; c=x+y; cout<<“addition is”<<c; P.P.Krishnaraj RSET
  • 29. Call by reference In call by reference method in place of calling a value to the function being called , a reference to the original variable is passed .i.e the same variable value can be accessed by any of the two names. add(a,b); } void add(int &x,int &y); { --------; } In function call We write reference variable for formal arguments &X,&Y will be the reference variable for a and b. if we change X and Y, Value of a and b are changed accordingly No need for return statement P.P.Krishnaraj RSET
  • 30. Program to illustrate call by reference #include<iostream.h> #include<conio.h> void swap(int &,int &); int main() { int a,b; cout<<“enter the values of a and b”; cin>>a>>b; cout<<“before swaping”; cout<<“A”<<a; cout<<“B”<<b; swap(a,b); cout<<“after swaping”; cout<<“A”<<a; cout<<“B”<<b; getch(); } void swap(int &X,&Y) { int temp; temp=X; X=Y; Y=X; } P.P.Krishnaraj RSET
  翻译: