SlideShare a Scribd company logo
Functions in c++
 Like c, c++ also contain a special function called
main().
 The main() function is an entry point of program
execution, it contain the code that tell computer what
to do as a programme execution.
 Because main() is function, you can write other
function within your source file and main() can call
them.
c++ lecture note by hansa halai
c++ lecture note by hansa halai
 Executable program
main()
{
// call functions..
someFunction1();
someFunction2();
}
someFunction2();
someFunction1();
Operating system
runs program
Return to OS
 In C++ , the main() returns value of type int to
operating system, so in c++ the main() function is
written as ,
int ()
int main(int arg1,char *argv[])
 So , if we write a return type int , we have to use return
statement inside the main() function, which are
written as:
int main()
{
…
return 0;
}
c++ lecture note by hansa halai
 Advantage of using functions:
o It help in reducing both physical and executable file size
in large program.
o Save memory, because all the calls to a function tell the
compiler to execute the same block of code.
o Enhances the program’s readability, understandability,
and maintainability.
o It helps in the reusability of the code.
c++ lecture note by hansa halai
 A function prototype tells the compiler the name of
function, the type of data returned by the
function, the number of parameter the function
expect to receive ,the type of parameters, and the
order in which these parameters are expected.
 The compiler use function prototypes to validate
function call.
 Syntax:
return_type function_name(argument_list);
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int max(int n1,int n2);
int main()
{
int n1;
int n2;
int a;
cout<<"Enter Number1: "<<"n";
cin>>n1;
cout<<"Enter Number2: "<<"n";
cin>>n2;
a=max(n1,n2);
cout<<"max value is "<<a<<"n";
return 0;
}
c++ lecture note by hansa halai
int max(int n1,int n2)
{
int result;
if(n1>n2)
result=n1;
else
result=n2;
return result;
}
 The c++ provides easy and effective use of reference
variable.
 When function is called, the caller creates a reference
for each argument and using references the actual
argument are accessed.
 This method of passing the arguments or parameters
to the function is called call by reference.
c++ lecture note by hansa halai
 To use the call by reference , function is written as:
int exch(int& a,int& b)
{
int t;
t=a;
a=b;
b=t;
}
When this function is called as :
exch(x , y);
It passes the arguments as
int & a=x;
int & b=y
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int exch(int&,int&);
int main()
{
int num1,num2;
cout<<"Enter Number1: ";
cin>>num1;
cout<<"Enter Number2: ";
cin>>num2;
cout<<"nBefore Swap:"<<"nn";
cout<<"Number1 is:"<<num1<<"n";
cout<<"Number2 is:"<<num2<<"n";
c++ lecture note by hansa halai
c++ lecture note by hansa halai
exch(num1,num2);
cout<<"nAfter Swap:"<<"nn";
cout<<"Number1 is:"<<num1<<"n";
cout<<"Number2 is:"<<num2<<"n";
return 0;
}
int exch(int& a,int& b)
{
int t;
t=a;
a=b;
b=t;
}
 Not only c++ function accepts the arguments as a
reference , but it can also return a reference.
 Like,
int& min(int& a, int& b)
{
return(a<b ? a:b);
}
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int& max(int&,int&);
int main()
{
int num1,num2,m;
cout<<"Enter Number1: ";
cin>>num1;
cout<<"Enter Number2: ";
cin>>num2;
c++ lecture note by hansa halai
m= max(num1,num2);
cout<<"max number is "<<m;
return 0;
}
int& max(int& a,int& b)
{
return(a>b?a:b);
}
c++ lecture note by hansa halai
 Values of actual
argument are passed to
dummy argument.
 Need extra memory to
store copy of value.
 Called function can not
access actual values in
caller function
 Reference are created for
actual argument.
 No need for extra
memory as only aliases
are created.
 Called function can
access the actual values
in caller function using
reference.
Call By Value Call By Reference
c++ lecture note by hansa halai
 Provide only one way
communication from
caller to called function.
 Slower as values are to be
copied.
 Simple and easy to
understand.
 Provide two way
communication between
caller and called function.
 Faster as no memory
operation required.
 Simple and easy to
understand
 We know that functions save memory space because all
calls to function cause the same code to be executed,
the function body need not to be duplicate in memory.
 When a compiler see a function call, it normally
generate a jump to the function, at end of the function,
it jump back to the instruction after the call.
 But when every time a function is called, it takes a lot of
extra time in executing a series of instructions for task
as jumping to the instruction.
c++ lecture note by hansa halai
 One solution for speedy execution and saving memory
in c++ is use inline function , it is used only for short
function.
 When use inline function , the function code in the
inline function body is inserted into the calling
function, instead of the control going to the function
definition and coming back.
 So function can not jump from its calling place. So it
will save the jumping time program execution is faster
compare to normal function. But it is used only for
short function, not use any loop inside it.
c++ lecture note by hansa halai
 You can declare an inline function before main() by
just placing the keyword inline before the normal
function.
 Syntax:
inline function_name(argument_list)
{
function_body
}
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
inline float mul(float x, float y)
{
return(x*y);
}
inline float div(float a, float b)
{
return(a/b);
}
c++ lecture note by hansa halai
int main()
{
float a,b;
cout<<"Enter number1:";
cin>>a;
cout<<"Enter number2:";
cin>>b;
cout<<"Answer of multiplication is "<<mul(a,b)<<"n";
cout<<"Answer of Division is "<<div(a,b)<<"n";
}
 Normally function is called with same number of
argument listed in function header.
 C++ differ in this matter and allows calling of function
with less number of argument then listed in its header.
 C++ make it possible by allowing default argument.
 Ex:
int max(int a,int b, int c=10);
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
int max(int,int,int c=10);
int main()
{
int a,b,p;
cout<<"Enter value:";
cin>>a>>b;
p=max(a,b);
cout<<"Max num is: "<<p<<endl;
return 0;
}
c++ lecture note by hansa halai
int max(int x,int y,int z)
{
int max =x;
if(y>max)
max=y;
if(z>max)
max=z;
return max;
}
 We can use const to make the arguments to the
function constant so that function can not change the
value of that argument.
 Like,
int fun(const int x)
{
int y;
x= x+1; // invalid
y=x; // valid
Return(y);
}
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
float circle(float,const float pi=3.14);
int main()
{
float r,area;
cout<<"Enter r:";
cin>>r;
area=circle(r);
cout<<"Area of circle is "<<area<<endl;
}
float circle(float r,float pi)
{
float c_area;
c_area=pi*r*r;
return c_area;
}
 The function overloading is ,same function name is
used for different task or different purpose it is also
called function polymorphism in oop.
 Using this concept, we can design a family of function
with same function name but it have different number
of arguments, different types of arguments and
different return type.
 All the function perform different operation
depending on matching of argument list in the
function call.
c++ lecture note by hansa halai
c++ lecture note by hansa halai
#include<iostream>
using namespace std;
const float pi=3.14;
float area(float r)
{
return(pi*r*r);
}
float area( float l, float b)
{
return(l*b);
}
int main()
{
float radius;
cout<<"Enter radius: ";
cin>>radius;
c++ lecture note by hansa halai
cout<<"Area of circle is : "<<area(radius)<<endl;
float length,breadth;
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
cout<<"Area of rectangle is: "<<area(length,breadth)<<endl;
return 0;
}
Functions in c++

More Related Content

PPT
Linked list
PPTX
Stacks IN DATA STRUCTURES
PPTX
Tokens expressionsin C++
PPTX
Threaded Binary Tree.pptx
PPTX
POLYNOMIAL ADDITION USING LINKED LIST.pptx
PPTX
Binary tree and operations
PPT
Bitwise operators
PPTX
linked list in data structure
Linked list
Stacks IN DATA STRUCTURES
Tokens expressionsin C++
Threaded Binary Tree.pptx
POLYNOMIAL ADDITION USING LINKED LIST.pptx
Binary tree and operations
Bitwise operators
linked list in data structure

What's hot (20)

PPTX
Header files of c++ unit 3 -topic 3
PPT
Pointers C programming
PPTX
Insertion sort
PPTX
Key and its different types
PPTX
DMBS Indexes.pptx
PPT
Recursion in c
PPTX
Functions In C
PPTX
Types of Constructor in C++
PPT
16717 functions in C++
 
PPTX
C++ language basic
PPTX
Binary Tree in Data Structure
PPT
18 hashing
PPSX
Type conversion
PPTX
String in programming language in c or c++
PPTX
Introduction to c programming language
PPT
Hashing PPT
PPT
Chapter 12 ds
PPTX
PPT
Header files of c++ unit 3 -topic 3
Pointers C programming
Insertion sort
Key and its different types
DMBS Indexes.pptx
Recursion in c
Functions In C
Types of Constructor in C++
16717 functions in C++
 
C++ language basic
Binary Tree in Data Structure
18 hashing
Type conversion
String in programming language in c or c++
Introduction to c programming language
Hashing PPT
Chapter 12 ds
Ad

Viewers also liked (20)

PDF
Matriz de Registro de Comunicación 1
PDF
Kit comunicación-entrada-3-oralidad
PPTX
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
PPTX
软件项目管理与团队合作
PDF
Simulacro examen censal 1
PPTX
expression in cpp
PPT
Business Card Designer
PPT
El Amor Nunca Deja De Ser
PDF
PPT
Digitalisation@Massey: Understanding the Real Drivers
PPT
C++ functions
PPT
Wind power
DOCX
Canal de distribuição
PPTX
exception handling in cpp
PPT
LEGADO UNIVERSAL DEL SHABBAT
PDF
Bautismo verdadero
PPT
PDF
Lavori Elisabetta Frameglia per corso graphic design
PPTX
Basics of oops concept
PPTX
[OOP - Lec 02] Why do we need OOP
Matriz de Registro de Comunicación 1
Kit comunicación-entrada-3-oralidad
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
软件项目管理与团队合作
Simulacro examen censal 1
expression in cpp
Business Card Designer
El Amor Nunca Deja De Ser
Digitalisation@Massey: Understanding the Real Drivers
C++ functions
Wind power
Canal de distribuição
exception handling in cpp
LEGADO UNIVERSAL DEL SHABBAT
Bautismo verdadero
Lavori Elisabetta Frameglia per corso graphic design
Basics of oops concept
[OOP - Lec 02] Why do we need OOP
Ad

Similar to Functions in c++ (20)

PPTX
Function in c program
PPTX
Fundamentals of functions in C program.pptx
PPTX
C function
PPTX
unit_2 (1).pptx
PPTX
Fundamental of programming Fundamental of programming
PPTX
Functionincprogram
PPTX
Functions
PPTX
unit_2.pptx
PPTX
6. Functions in C ++ programming object oriented programming
DOCX
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
PDF
Unit 3 (1)
PDF
Programming Fundamentals Functions in C and types
PPTX
Function in c
PDF
PPTX
PDF
unit3 part2 pcds function notes.pdf
PPT
PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
UNIT3.pptx
PPT
Material 3 (4).ppt this ppt is about the
Function in c program
Fundamentals of functions in C program.pptx
C function
unit_2 (1).pptx
Fundamental of programming Fundamental of programming
Functionincprogram
Functions
unit_2.pptx
6. Functions in C ++ programming object oriented programming
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
Unit 3 (1)
Programming Fundamentals Functions in C and types
Function in c
unit3 part2 pcds function notes.pdf
Chapter 1 (2) array and structure r.pptx
UNIT3.pptx
Material 3 (4).ppt this ppt is about the

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Supply Chain Operations Speaking Notes -ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
PPH.pptx obstetrics and gynecology in nursing
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Institutional Correction lecture only . . .
Anesthesia in Laparoscopic Surgery in India
Insiders guide to clinical Medicine.pdf
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF

Functions in c++

  • 2.  Like c, c++ also contain a special function called main().  The main() function is an entry point of program execution, it contain the code that tell computer what to do as a programme execution.  Because main() is function, you can write other function within your source file and main() can call them. c++ lecture note by hansa halai
  • 3. c++ lecture note by hansa halai  Executable program main() { // call functions.. someFunction1(); someFunction2(); } someFunction2(); someFunction1(); Operating system runs program Return to OS
  • 4.  In C++ , the main() returns value of type int to operating system, so in c++ the main() function is written as , int () int main(int arg1,char *argv[])  So , if we write a return type int , we have to use return statement inside the main() function, which are written as: int main() { … return 0; } c++ lecture note by hansa halai
  • 5.  Advantage of using functions: o It help in reducing both physical and executable file size in large program. o Save memory, because all the calls to a function tell the compiler to execute the same block of code. o Enhances the program’s readability, understandability, and maintainability. o It helps in the reusability of the code. c++ lecture note by hansa halai
  • 6.  A function prototype tells the compiler the name of function, the type of data returned by the function, the number of parameter the function expect to receive ,the type of parameters, and the order in which these parameters are expected.  The compiler use function prototypes to validate function call.  Syntax: return_type function_name(argument_list); c++ lecture note by hansa halai
  • 7. c++ lecture note by hansa halai #include<iostream> using namespace std; int max(int n1,int n2); int main() { int n1; int n2; int a; cout<<"Enter Number1: "<<"n"; cin>>n1; cout<<"Enter Number2: "<<"n"; cin>>n2; a=max(n1,n2); cout<<"max value is "<<a<<"n"; return 0; }
  • 8. c++ lecture note by hansa halai int max(int n1,int n2) { int result; if(n1>n2) result=n1; else result=n2; return result; }
  • 9.  The c++ provides easy and effective use of reference variable.  When function is called, the caller creates a reference for each argument and using references the actual argument are accessed.  This method of passing the arguments or parameters to the function is called call by reference. c++ lecture note by hansa halai
  • 10.  To use the call by reference , function is written as: int exch(int& a,int& b) { int t; t=a; a=b; b=t; } When this function is called as : exch(x , y); It passes the arguments as int & a=x; int & b=y c++ lecture note by hansa halai
  • 11. #include<iostream> using namespace std; int exch(int&,int&); int main() { int num1,num2; cout<<"Enter Number1: "; cin>>num1; cout<<"Enter Number2: "; cin>>num2; cout<<"nBefore Swap:"<<"nn"; cout<<"Number1 is:"<<num1<<"n"; cout<<"Number2 is:"<<num2<<"n"; c++ lecture note by hansa halai
  • 12. c++ lecture note by hansa halai exch(num1,num2); cout<<"nAfter Swap:"<<"nn"; cout<<"Number1 is:"<<num1<<"n"; cout<<"Number2 is:"<<num2<<"n"; return 0; } int exch(int& a,int& b) { int t; t=a; a=b; b=t; }
  • 13.  Not only c++ function accepts the arguments as a reference , but it can also return a reference.  Like, int& min(int& a, int& b) { return(a<b ? a:b); } c++ lecture note by hansa halai
  • 14. c++ lecture note by hansa halai #include<iostream> using namespace std; int& max(int&,int&); int main() { int num1,num2,m; cout<<"Enter Number1: "; cin>>num1; cout<<"Enter Number2: "; cin>>num2;
  • 15. c++ lecture note by hansa halai m= max(num1,num2); cout<<"max number is "<<m; return 0; } int& max(int& a,int& b) { return(a>b?a:b); }
  • 16. c++ lecture note by hansa halai  Values of actual argument are passed to dummy argument.  Need extra memory to store copy of value.  Called function can not access actual values in caller function  Reference are created for actual argument.  No need for extra memory as only aliases are created.  Called function can access the actual values in caller function using reference. Call By Value Call By Reference
  • 17. c++ lecture note by hansa halai  Provide only one way communication from caller to called function.  Slower as values are to be copied.  Simple and easy to understand.  Provide two way communication between caller and called function.  Faster as no memory operation required.  Simple and easy to understand
  • 18.  We know that functions save memory space because all calls to function cause the same code to be executed, the function body need not to be duplicate in memory.  When a compiler see a function call, it normally generate a jump to the function, at end of the function, it jump back to the instruction after the call.  But when every time a function is called, it takes a lot of extra time in executing a series of instructions for task as jumping to the instruction. c++ lecture note by hansa halai
  • 19.  One solution for speedy execution and saving memory in c++ is use inline function , it is used only for short function.  When use inline function , the function code in the inline function body is inserted into the calling function, instead of the control going to the function definition and coming back.  So function can not jump from its calling place. So it will save the jumping time program execution is faster compare to normal function. But it is used only for short function, not use any loop inside it. c++ lecture note by hansa halai
  • 20.  You can declare an inline function before main() by just placing the keyword inline before the normal function.  Syntax: inline function_name(argument_list) { function_body } c++ lecture note by hansa halai
  • 21. c++ lecture note by hansa halai #include<iostream> using namespace std; inline float mul(float x, float y) { return(x*y); } inline float div(float a, float b) { return(a/b); }
  • 22. c++ lecture note by hansa halai int main() { float a,b; cout<<"Enter number1:"; cin>>a; cout<<"Enter number2:"; cin>>b; cout<<"Answer of multiplication is "<<mul(a,b)<<"n"; cout<<"Answer of Division is "<<div(a,b)<<"n"; }
  • 23.  Normally function is called with same number of argument listed in function header.  C++ differ in this matter and allows calling of function with less number of argument then listed in its header.  C++ make it possible by allowing default argument.  Ex: int max(int a,int b, int c=10); c++ lecture note by hansa halai
  • 24. c++ lecture note by hansa halai #include<iostream> using namespace std; int max(int,int,int c=10); int main() { int a,b,p; cout<<"Enter value:"; cin>>a>>b; p=max(a,b); cout<<"Max num is: "<<p<<endl; return 0; }
  • 25. c++ lecture note by hansa halai int max(int x,int y,int z) { int max =x; if(y>max) max=y; if(z>max) max=z; return max; }
  • 26.  We can use const to make the arguments to the function constant so that function can not change the value of that argument.  Like, int fun(const int x) { int y; x= x+1; // invalid y=x; // valid Return(y); } c++ lecture note by hansa halai
  • 27. c++ lecture note by hansa halai #include<iostream> using namespace std; float circle(float,const float pi=3.14); int main() { float r,area; cout<<"Enter r:"; cin>>r; area=circle(r); cout<<"Area of circle is "<<area<<endl; } float circle(float r,float pi) { float c_area; c_area=pi*r*r; return c_area; }
  • 28.  The function overloading is ,same function name is used for different task or different purpose it is also called function polymorphism in oop.  Using this concept, we can design a family of function with same function name but it have different number of arguments, different types of arguments and different return type.  All the function perform different operation depending on matching of argument list in the function call. c++ lecture note by hansa halai
  • 29. c++ lecture note by hansa halai #include<iostream> using namespace std; const float pi=3.14; float area(float r) { return(pi*r*r); } float area( float l, float b) { return(l*b); } int main() { float radius; cout<<"Enter radius: "; cin>>radius;
  • 30. c++ lecture note by hansa halai cout<<"Area of circle is : "<<area(radius)<<endl; float length,breadth; cout<<"Enter length: "; cin>>length; cout<<"Enter breadth: "; cin>>breadth; cout<<"Area of rectangle is: "<<area(length,breadth)<<endl; return 0; }