Lec -9 Functions in a Class 2 (1)
Lec -9 Functions in a Class 2 (1)
Programming
Mohona Ghosh
Inline Functions
When the program executes the function call instruction the CPU stores the memory address of the
instruction following the function call, copies the arguments of the function on the stack and finally transfers
control to the specified function. The CPU then executes the function code, stores the function return value
in a predefined memory location/register and returns control to the calling function.
• https://www.youtube.com/watch?v=ygK0YON10sQ
Inline Functions
• This can become overhead if the execution time of function is less than the switching
time from the caller function to called function (callee).
• For functions that are large and/or perform complex tasks, the overhead of the function
call is usually insignificant compared to the amount of time the function takes to run.
However, for small, commonly-used functions, the time needed to make the function call
is often a lot more than the time needed to actually execute the function’s code. This
overhead occurs for small functions because execution time of small function is less than
the switching time.
• C++ provides an inline functions to reduce the function call overhead.
• Inline function is a function that is expanded in line when it is called. When the inline
function is called whole code of the inline function gets inserted or substituted at the
point of inline function call. This substitution is performed by the C++ compiler at
compile time.
• So the overhead of function calling is reduced
• Inline function may increase efficiency if it is small.
Inline Functions
Inline Functions
Some Important points about Inline Functions
• All the functions defined inside class definition are by default
inline
• We must keep inline functions small, small inline functions have better
efficiency.
• Inline functions do increase efficiency, but we should not make all the
functions inline. Because if we make large functions inline, it may lead to
code bloat, i.e., makes the program to take up more memory because the
statements that define the inline function are reproduced at each point
where the function is called and this might affect the speed too.
• Hence, it is advised to define large functions outside the class definition
using scope resolution :: operator, because if we define such functions
inside class definition, then they become inline automatically.
Inline Functions
Friend Function
Friend Function
class B
{
int b1 = 10;
public:
friend int add(A, B);
};
int add(A a, B b)
{
return a.a1 + b.b1;
}
Default Arguments
Constant Arguments
• In C++, an argument to a function can be declared as const.
• The argument with constant value should be initialized during the
function declaration.
• Syntax
type function_name(const data_type variable_name=value);
• For example
int max(const int a=3, int b); //function prototype
• The qualifier const tells the compiler that the value of that argument
cannot be modified, and any such attempt will generate a compile
time error.
OUTPUT:
10
0