SlideShare a Scribd company logo
5
Most read
7
Most read
8
Most read
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

PPTX
Polymorphism In c++
PPTX
classes and objects in C++
PPTX
PPT
Function overloading(c++)
PDF
Managing I/O in c++
PPT
Class and object in C++
PDF
Function overloading ppt
PPTX
07. Virtual Functions
Polymorphism In c++
classes and objects in C++
Function overloading(c++)
Managing I/O in c++
Class and object in C++
Function overloading ppt
07. Virtual Functions

What's hot (20)

PPTX
Functions in c
PDF
Constructor and Destructor
PPTX
Templates in c++
PPTX
Static Data Members and Member Functions
PPTX
Member Function in C++
PPT
C++ Arrays
PPT
One Dimensional Array
PPTX
Tokens in C++
PPTX
Operators and expressions in C++
PPT
friend function(c++)
PPTX
Constructor and Types of Constructors
PPTX
Inline function
PPTX
INLINE FUNCTION IN C++
PPTX
Oop c++class(final).ppt
PDF
Constructors and Destructors
PPTX
Typedef
PDF
PPTX
Classes and objects
PPTX
virtual function
PPT
08 c++ Operator Overloading.ppt
Functions in c
Constructor and Destructor
Templates in c++
Static Data Members and Member Functions
Member Function in C++
C++ Arrays
One Dimensional Array
Tokens in C++
Operators and expressions in C++
friend function(c++)
Constructor and Types of Constructors
Inline function
INLINE FUNCTION IN C++
Oop c++class(final).ppt
Constructors and Destructors
Typedef
Classes and objects
virtual function
08 c++ Operator Overloading.ppt
Ad

Viewers also liked (20)

PPTX
functions of C++
PPT
C++ Function
PPTX
Function C++
PPT
FUNCTIONS IN c++ PPT
PPT
Functions in C++
PPT
Friends function and_classes
PPTX
Inline function in C++
PPT
Functions
PPT
Functions in C++
PPTX
PDF
Chapter23 friend-function-friend-class
PPTX
Object as function argument , friend and static function by shahzad younas
PPT
RECURSION IN C
PPTX
Safety rules and earthing
PPT
Simplifies and normal forms - Theory of Computation
PPTX
Friend functions
PPT
Linear function and slopes of a line
PPT
Software Coding- Software Coding
PPT
Arrays Basics
PDF
03 function overloading
functions of C++
C++ Function
Function C++
FUNCTIONS IN c++ PPT
Functions in C++
Friends function and_classes
Inline function in C++
Functions
Functions in C++
Chapter23 friend-function-friend-class
Object as function argument , friend and static function by shahzad younas
RECURSION IN C
Safety rules and earthing
Simplifies and normal forms - Theory of Computation
Friend functions
Linear function and slopes of a line
Software Coding- Software Coding
Arrays Basics
03 function overloading
Ad

Similar to Inline Functions and Default arguments (20)

PPT
Material 3 (4).ppt this ppt is about the
PPT
Function
PPTX
Python_Functions_Unit1.pptx
PPTX
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPTX
Chapter 4
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PPTX
CH.4FUNCTIONS IN C (1).pptx
PPT
16717 functions in C++
 
PPTX
Functions in C++ (OOP)
PPT
PDF
PPTX
Functions in Python Programming Language
PDF
10. funtions and closures IN SWIFT PROGRAMMING
PPTX
Lab 2 Math functions c++ programming.pptx
PPT
Review functions
PPTX
Learning C++ - Functions in C++ 3
PPT
Inbuilt Functions in C++ computer language.ppt
PPTX
Function in C++, Methods in C++ coding programming
PPTX
functioninpython-1.pptx
Material 3 (4).ppt this ppt is about the
Function
Python_Functions_Unit1.pptx
Chp8_C++_Functions_Part2_User-defined functions.pptx
Chapter 4
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
CH.4FUNCTIONS IN C (1).pptx
16717 functions in C++
 
Functions in C++ (OOP)
Functions in Python Programming Language
10. funtions and closures IN SWIFT PROGRAMMING
Lab 2 Math functions c++ programming.pptx
Review functions
Learning C++ - Functions in C++ 3
Inbuilt Functions in C++ computer language.ppt
Function in C++, Methods in C++ coding programming
functioninpython-1.pptx

More from Nikhil Pandit (6)

PPT
Use case Diagram and Sequence Diagram
PPTX
The scope of contribution
PPTX
4 stroke diesel engine
PPT
CHAIN RULE AND IMPLICIT FUNCTION
PPTX
Register transfer and micro-operation
PPTX
Spyware and rootkit
Use case Diagram and Sequence Diagram
The scope of contribution
4 stroke diesel engine
CHAIN RULE AND IMPLICIT FUNCTION
Register transfer and micro-operation
Spyware and rootkit

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
additive manufacturing of ss316l using mig welding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
composite construction of structures.pdf
PPT
Project quality management in manufacturing
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Well-logging-methods_new................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
573137875-Attendance-Management-System-original
PPTX
Sustainable Sites - Green Building Construction
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Welding lecture in detail for understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
additive manufacturing of ss316l using mig welding
Model Code of Practice - Construction Work - 21102022 .pdf
composite construction of structures.pdf
Project quality management in manufacturing
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Well-logging-methods_new................
Embodied AI: Ushering in the Next Era of Intelligent Systems
573137875-Attendance-Management-System-original
Sustainable Sites - Green Building Construction
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mechanical Engineering MATERIALS Selection
CYBER-CRIMES AND SECURITY A guide to understanding
Welding lecture in detail for understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
bas. eng. economics group 4 presentation 1.pptx

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.