Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views38 pages

Chapter 3 Functions in C++

Chapter 3 of the document discusses functions in C++, including their definition, the use of parameters, and the concept of inline functions for performance optimization. It highlights the advantages and disadvantages of inline functions, as well as function overloading and default arguments, which enhance code readability and flexibility. The chapter emphasizes the importance of functions in reusing code and improving program efficiency.

Uploaded by

raymon.aryal728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views38 pages

Chapter 3 Functions in C++

Chapter 3 of the document discusses functions in C++, including their definition, the use of parameters, and the concept of inline functions for performance optimization. It highlights the advantages and disadvantages of inline functions, as well as function overloading and default arguments, which enhance code readability and flexibility. The chapter emphasizes the importance of functions in reusing code and improving program efficiency.

Uploaded by

raymon.aryal728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

OBJECT ORIENTED

PROGRAMMING
Chapter 3: Functions in C++

Er. Sudeeip Bhandarri


C++ Functions
● A function is a block of code which only runs

when it is called.

● You can pass data, known as parameters, into a

function.

● Functions are used to perform certain actions, and


C++ Functions
they are important for reusing code: Define the

code once, and use it many times.


Arguments and Parameters
Inline Functions in C++
● In C++, inline functions provide a way to
optimize the performance of the program by
reducing the overhead related to a function call.

Inline
● When a function is specified as inline the whole Functions
code of the inline function is inserted or
substituted at the point of its call during the
compilation instead of using the normal function
call mechanism.
Need for Inline Functions
● When a function is called, the CPU stores the return address, copies arguments to

the call stack, and transfers control to the function.

● After execution, the return value is stored, and control is returned to the caller. This

overhead can be significant for small, frequently used functions, as their execution

time is less than the time spent on the call and return process.

● This is where the inline functions shine. They remove this overhead by substituting

the code of the function in place of a function call.


Inline Functions in
Class
Inline Functions vs Without Inline Functions
Advantages of Inline Functions
Advantages of Inline Functions
● Function call overhead doesn't occur.
● When you inline a function, you may enable the compiler to perform
context-specific optimization on the body of the function. Such optimizations
are not possible for normal function calls. Other optimizations can be
obtained by considering the flows of the calling context and the called
context.
● An inline function may be useful (if it is small) for embedded systems
because an inline function can yield less code than the function called
preamble and return.
Disadvantages of Inline Functions
Disadvantages of Inline Functions
● When an inline function body is substituted at the point of the function call, the total number
of variables used by the function also gets inserted. So, the number of registers going to be
used for the variables will also get increased.
● Inline functions might cause thrashing because inlining might increase the size of the binary
executable file.
● Too much inlining can reduce your instruction cache hit rate, thus reducing the speed of
instruction fetch from that of cache memory to that of primary memory.
● If someone changes the code inside the inline function, then all the calling location has to be
recompiled because the compiler would be required to replace all the code once again to
reflect the changes.
Functions Overloading in C++
● C++ function overloading allows you to define multiple functions
with the same name but different parameters.

● It is a form of compile time polymorphism in which a function can


perform different jobs based on the different parameters passed to it.

● It is a feature of object-oriented programming that increases the


readability of the program.
Reason for function overloading
Different Ways of Function Overloading

A function in C++ can be overloaded in three different ways:

● By having different number of parameters.


● By having different types of parameters.
● By having both different number and types of parameters.
Different Number of Parameters

Output
20
120
Different Types of Parameters
Different Number and Types of Parameters
Default Arguments in C++
● A default argument is a value provided for a parameter in a
function declaration that is automatically assigned by the
compiler if no value is provided for those parameters in function
call.

● If the value is passed for it, the default value is overwritten by


the passed value.
Default Arguments Syntax
When to use Default arguments??
Advantages of Default Arguments
Disadvantages of Default Arguments

You might also like