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

More Related Content

What's hot (20)

Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
Munni28
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Exception handling
Exception handlingException handling
Exception handling
PhD Research Scholar
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
Dr. C.V. Suresh Babu
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Sangharsh agarwal
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
Rust: Systems Programming for Everyone
Rust: Systems Programming for EveryoneRust: Systems Programming for Everyone
Rust: Systems Programming for Everyone
C4Media
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
Jan Rüegg
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
Neeru Mittal
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
Munni28
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
Rust: Systems Programming for Everyone
Rust: Systems Programming for EveryoneRust: Systems Programming for Everyone
Rust: Systems Programming for Everyone
C4Media
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
Jan Rüegg
 

Viewers also liked (20)

Matriz de Registro de Comunicación 1
Matriz de Registro de Comunicación 1Matriz de Registro de Comunicación 1
Matriz de Registro de Comunicación 1
Gerson Ames
 
Kit comunicación-entrada-3-oralidad
Kit comunicación-entrada-3-oralidadKit comunicación-entrada-3-oralidad
Kit comunicación-entrada-3-oralidad
Gerson Ames
 
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
Uni Papua Football
 
软件项目管理与团队合作
软件项目管理与团队合作软件项目管理与团队合作
软件项目管理与团队合作
晟 沈
 
Simulacro examen censal 1
Simulacro examen censal 1Simulacro examen censal 1
Simulacro examen censal 1
I.E.P TERCER MILENIO
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
gourav kottawar
 
Business Card Designer
Business Card DesignerBusiness Card Designer
Business Card Designer
NextBee Media
 
El Amor Nunca Deja De Ser
El Amor Nunca Deja De SerEl Amor Nunca Deja De Ser
El Amor Nunca Deja De Ser
Tabernáculo De Adoración Adonay
 
2009
20092009
2009
Sonia Sanchez
 
Digitalisation@Massey: Understanding the Real Drivers
Digitalisation@Massey: Understanding the Real DriversDigitalisation@Massey: Understanding the Real Drivers
Digitalisation@Massey: Understanding the Real Drivers
Mark Brown
 
C++ functions
C++ functionsC++ functions
C++ functions
Dawood Jutt
 
Wind power
Wind powerWind power
Wind power
jacattackhk
 
Canal de distribuição
Canal de distribuiçãoCanal de distribuição
Canal de distribuição
Reinaldo Mariano
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
gourav kottawar
 
LEGADO UNIVERSAL DEL SHABBAT
LEGADO UNIVERSAL DEL SHABBATLEGADO UNIVERSAL DEL SHABBAT
LEGADO UNIVERSAL DEL SHABBAT
Ricardo Mojica
 
Bautismo verdadero
Bautismo verdaderoBautismo verdadero
Bautismo verdadero
julio torres
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
Praveen M Jigajinni
 
Lavori Elisabetta Frameglia per corso graphic design
Lavori Elisabetta Frameglia per corso graphic designLavori Elisabetta Frameglia per corso graphic design
Lavori Elisabetta Frameglia per corso graphic design
NAD Nuova Accademia del Design
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
DINESH KUMAR ARIVARASAN
 
[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP
Muhammad Hammad Waseem
 
Matriz de Registro de Comunicación 1
Matriz de Registro de Comunicación 1Matriz de Registro de Comunicación 1
Matriz de Registro de Comunicación 1
Gerson Ames
 
Kit comunicación-entrada-3-oralidad
Kit comunicación-entrada-3-oralidadKit comunicación-entrada-3-oralidad
Kit comunicación-entrada-3-oralidad
Gerson Ames
 
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
Uni papua fc kuta gle aceh held exhibition match with putra seulawah fc.
Uni Papua Football
 
软件项目管理与团队合作
软件项目管理与团队合作软件项目管理与团队合作
软件项目管理与团队合作
晟 沈
 
Business Card Designer
Business Card DesignerBusiness Card Designer
Business Card Designer
NextBee Media
 
Digitalisation@Massey: Understanding the Real Drivers
Digitalisation@Massey: Understanding the Real DriversDigitalisation@Massey: Understanding the Real Drivers
Digitalisation@Massey: Understanding the Real Drivers
Mark Brown
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
gourav kottawar
 
LEGADO UNIVERSAL DEL SHABBAT
LEGADO UNIVERSAL DEL SHABBATLEGADO UNIVERSAL DEL SHABBAT
LEGADO UNIVERSAL DEL SHABBAT
Ricardo Mojica
 
Bautismo verdadero
Bautismo verdaderoBautismo verdadero
Bautismo verdadero
julio torres
 
Ad

Similar to Functions in c++ (20)

Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
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
 
C function
C functionC function
C function
thirumalaikumar3
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
Functions
FunctionsFunctions
Functions
PralhadKhanal1
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming6. Functions in C ++ programming object oriented programming
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONSPPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
PPS 6.6.FUNCTION INTRODUCTION & WRITING FUNCTIONS, SCOPE OF VARIABLES FUNCTIONS
Sitamarhi Institute of Technology
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
Function in c
Function in cFunction in c
Function in c
CGC Technical campus,Mohali
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
Chapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptxChapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
NabeelaNousheen
 
Ad

Recently uploaded (20)

he Grant Preparation Playbook: Building a System for Grant Success
he Grant Preparation Playbook: Building a System for Grant Successhe Grant Preparation Playbook: Building a System for Grant Success
he Grant Preparation Playbook: Building a System for Grant Success
TechSoup
 
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...
EduSkills OECD
 
From Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI StrategyFrom Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI Strategy
victoriamangiantini1
 
NS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptxNS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptx
manuelaromero2013
 
Flower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdfFlower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdf
kushallamichhame
 
TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...
TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...
TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...
TechSoup
 
Online elections for Parliament for European Union
Online elections for Parliament for European UnionOnline elections for Parliament for European Union
Online elections for Parliament for European Union
Monica Enache
 
Letter to Secretary Linda McMahon from U.S. Senators
Letter to Secretary Linda McMahon from U.S. SenatorsLetter to Secretary Linda McMahon from U.S. Senators
Letter to Secretary Linda McMahon from U.S. Senators
Mebane Rash
 
CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...
CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...
CANSA World No Tobacco Day campaign 2025 Vaping is not a safe form of smoking...
CANSA The Cancer Association of South Africa
 
The Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdfThe Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdf
Mirza Gazanfar Ali Baig
 
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the BoardThe Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
victoriamangiantini1
 
How to Automate Activities Using Odoo 18 CRM
How to Automate Activities Using Odoo 18 CRMHow to Automate Activities Using Odoo 18 CRM
How to Automate Activities Using Odoo 18 CRM
Celine George
 
EDI as Scientific Problem, Professor Nira Chamberlain OBE
EDI as Scientific Problem, Professor Nira Chamberlain OBEEDI as Scientific Problem, Professor Nira Chamberlain OBE
EDI as Scientific Problem, Professor Nira Chamberlain OBE
Association for Project Management
 
How to Manage Allow Ship Later for Sold Product in odoo Point of Sale
How to Manage Allow Ship Later for Sold Product in odoo Point of SaleHow to Manage Allow Ship Later for Sold Product in odoo Point of Sale
How to Manage Allow Ship Later for Sold Product in odoo Point of Sale
Celine George
 
From Building Products to Owning the Business
From Building Products to Owning the BusinessFrom Building Products to Owning the Business
From Building Products to Owning the Business
victoriamangiantini1
 
Product in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against YouProduct in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against You
victoriamangiantini1
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
The Pedagogy We Practice: Best Practices for Critical Instructional Design
The Pedagogy We Practice: Best Practices for Critical Instructional DesignThe Pedagogy We Practice: Best Practices for Critical Instructional Design
The Pedagogy We Practice: Best Practices for Critical Instructional Design
Sean Michael Morris
 
Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18
Celine George
 
How to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo SlidesHow to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo Slides
Celine George
 
he Grant Preparation Playbook: Building a System for Grant Success
he Grant Preparation Playbook: Building a System for Grant Successhe Grant Preparation Playbook: Building a System for Grant Success
he Grant Preparation Playbook: Building a System for Grant Success
TechSoup
 
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...
Launch of The State of Global Teenage Career Preparation - Andreas Schleicher...
EduSkills OECD
 
From Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI StrategyFrom Hype to Moat: Building a Defensible AI Strategy
From Hype to Moat: Building a Defensible AI Strategy
victoriamangiantini1
 
NS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptxNS3 Unit 5 Energy presentation 2025.pptx
NS3 Unit 5 Energy presentation 2025.pptx
manuelaromero2013
 
Flower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdfFlower Identification Class-10 by Kushal Lamichhane.pdf
Flower Identification Class-10 by Kushal Lamichhane.pdf
kushallamichhame
 
TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...
TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...
TechSoup - Microsoft Discontinuation of Selected Cloud Donated Offers 2025.05...
TechSoup
 
Online elections for Parliament for European Union
Online elections for Parliament for European UnionOnline elections for Parliament for European Union
Online elections for Parliament for European Union
Monica Enache
 
Letter to Secretary Linda McMahon from U.S. Senators
Letter to Secretary Linda McMahon from U.S. SenatorsLetter to Secretary Linda McMahon from U.S. Senators
Letter to Secretary Linda McMahon from U.S. Senators
Mebane Rash
 
The Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdfThe Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdf
Mirza Gazanfar Ali Baig
 
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the BoardThe Board Doesn’t Care About Your Roadmap: Running Product at the Board
The Board Doesn’t Care About Your Roadmap: Running Product at the Board
victoriamangiantini1
 
How to Automate Activities Using Odoo 18 CRM
How to Automate Activities Using Odoo 18 CRMHow to Automate Activities Using Odoo 18 CRM
How to Automate Activities Using Odoo 18 CRM
Celine George
 
How to Manage Allow Ship Later for Sold Product in odoo Point of Sale
How to Manage Allow Ship Later for Sold Product in odoo Point of SaleHow to Manage Allow Ship Later for Sold Product in odoo Point of Sale
How to Manage Allow Ship Later for Sold Product in odoo Point of Sale
Celine George
 
From Building Products to Owning the Business
From Building Products to Owning the BusinessFrom Building Products to Owning the Business
From Building Products to Owning the Business
victoriamangiantini1
 
Product in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against YouProduct in Wartime: How to Build When the Market Is Against You
Product in Wartime: How to Build When the Market Is Against You
victoriamangiantini1
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
The Pedagogy We Practice: Best Practices for Critical Instructional Design
The Pedagogy We Practice: Best Practices for Critical Instructional DesignThe Pedagogy We Practice: Best Practices for Critical Instructional Design
The Pedagogy We Practice: Best Practices for Critical Instructional Design
Sean Michael Morris
 
Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18Automated Actions (Automation) in the Odoo 18
Automated Actions (Automation) in the Odoo 18
Celine George
 
How to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo SlidesHow to Manage Blanket Order in Odoo 18 - Odoo Slides
How to Manage Blanket Order in Odoo 18 - Odoo Slides
Celine George
 

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; }
  翻译: