SlideShare a Scribd company logo
1 of 10
Functions
• A function is a group of statements that together
perform a task.
• main() -> mandatory function =
• Various programs define additional functions.
• A function declaration tells the compiler about a
function's name, return type, and parameters. A
function definition provides the actual body of the
function.
Function Declaration
• A function can be defined as:
Syntax : return_type function_name(arguments)
{
body of the function
}
For e.g :
int add(int a,int b)
{
return a+b;
}
Calling a Function
• A function can be called
in a main() function or in
other functions.
• When it is called inside
itself than it called as
recursive function.
main()/function_1()
{
body
function(passing values);
}
For e.g :
main()
{
int a,b;
cout<<“Sum=“<<add(a,b);
}
int add(int x,int y)
{
return x+y;
}
Inline Functions
• If a function is inline than:
– Compiler puts its code at the place
where it is called at compile time
– To define a function as inline
function
• use the keyword “inline” just
before the return type.
– The compiler ignore the inline
qualifier in case defined function is
more than a line.
inline return_type
function_name(args)
{
//one line code
}
Inline Function (example)
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
// Main function for the program
int main( )
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) <<
endl;
return 0;
}
Output:
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
Why Inline functions
• Objective of using functions:
– To save memory space, when a
function is likely to be called
many times.
Jumping to a function
Saving the registers
Pushing arguments in to the
stack
Returning to the calling
function
• When a function is small enough
(only one line of code) these
things would be a wastage of
time and resources.
Function with Default Arguments
• A default argument is a value
provided in function
declaration that is
automatically assigned by the
compiler if caller of the
function doesn’t provide a
value for the argument with
default value.
return _type f_name (arg1,arg2,arg3=value)
• When a default argument is placed then all the other arguments
after that must be default arguments only.
• That says the default arguments must placed on right most side of
all the arguments collectively
Default arguments (example)
int sum(int x, int y, int z=0, int w=0)
{
return (x + y + z + w);
}
int main()
{
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) <<
endl;
return 0;
}
Output:
25
50
80
Default Arguments
• They may come in handy when some arguments
are having the same values.
– For e.g. Bank interest may remain the same for all
the customers for a particular period of deposit.
Inline Functions and Default arguments

More Related Content

What's hot

Call by value
Call by valueCall by value
Call by value
Dharani G
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 

What's hot (20)

Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
virtual function
virtual functionvirtual function
virtual function
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Call by value
Call by valueCall by value
Call by value
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
class and objects
class and objectsclass and objects
class and objects
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 

Viewers also liked

C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
asadsardar
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Linear function and slopes of a line
Linear function and slopes of a lineLinear function and slopes of a line
Linear function and slopes of a line
Jerlyn Fernandez
 

Viewers also liked (20)

functions of C++
functions of C++functions of C++
functions of C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Function C++
Function C++ Function C++
Function C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Functions
FunctionsFunctions
Functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Diodes
DiodesDiodes
Diodes
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Safety rules and earthing
Safety rules and earthingSafety rules and earthing
Safety rules and earthing
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of Computation
 
Friend functions
Friend functions Friend functions
Friend functions
 
Linear function and slopes of a line
Linear function and slopes of a lineLinear function and slopes of a line
Linear function and slopes of a line
 
Software Coding- Software Coding
Software Coding- Software CodingSoftware Coding- Software Coding
Software Coding- Software Coding
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 

Similar to Inline Functions and Default arguments

Similar to Inline Functions and Default arguments (20)

Function
FunctionFunction
Function
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
Functions
FunctionsFunctions
Functions
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Review functions
Review functionsReview functions
Review functions
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Function
Function Function
Function
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 

More from Nikhil Pandit (6)

Use case Diagram and Sequence Diagram
Use case Diagram and Sequence DiagramUse case Diagram and Sequence Diagram
Use case Diagram and Sequence Diagram
 
The scope of contribution
The scope of contributionThe scope of contribution
The scope of contribution
 
4 stroke diesel engine
4 stroke diesel engine4 stroke diesel engine
4 stroke diesel engine
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTION
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
 
Spyware and rootkit
Spyware and rootkitSpyware and rootkit
Spyware and rootkit
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 

Inline Functions and Default arguments

  • 1. Functions • A function is a group of statements that together perform a task. • main() -> mandatory function = • Various programs define additional functions. • A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
  • 2. Function Declaration • A function can be defined as: Syntax : return_type function_name(arguments) { body of the function } For e.g : int add(int a,int b) { return a+b; }
  • 3. Calling a Function • A function can be called in a main() function or in other functions. • When it is called inside itself than it called as recursive function. main()/function_1() { body function(passing values); } For e.g : main() { int a,b; cout<<“Sum=“<<add(a,b); } int add(int x,int y) { return x+y; }
  • 4. Inline Functions • If a function is inline than: – Compiler puts its code at the place where it is called at compile time – To define a function as inline function • use the keyword “inline” just before the return type. – The compiler ignore the inline qualifier in case defined function is more than a line. inline return_type function_name(args) { //one line code }
  • 5. Inline Function (example) #include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } Output: Max (20,10): 20 Max (0,200): 200 Max (100,1010): 1010
  • 6. Why Inline functions • Objective of using functions: – To save memory space, when a function is likely to be called many times. Jumping to a function Saving the registers Pushing arguments in to the stack Returning to the calling function • When a function is small enough (only one line of code) these things would be a wastage of time and resources.
  • 7. Function with Default Arguments • A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value. return _type f_name (arg1,arg2,arg3=value) • When a default argument is placed then all the other arguments after that must be default arguments only. • That says the default arguments must placed on right most side of all the arguments collectively
  • 8. Default arguments (example) int sum(int x, int y, int z=0, int w=0) { return (x + y + z + w); } int main() { cout << sum(10, 15) << endl; cout << sum(10, 15, 25) << endl; cout << sum(10, 15, 25, 30) << endl; return 0; } Output: 25 50 80
  • 9. Default Arguments • They may come in handy when some arguments are having the same values. – For e.g. Bank interest may remain the same for all the customers for a particular period of deposit.