-
Notifications
You must be signed in to change notification settings - Fork 7
/
inlineFunction.cpp
28 lines (25 loc) · 1.06 KB
/
inlineFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
inline int square(int x);
int main() {
/*
What is the need of inline function?
--> Every time a function is called, it takes some time in context switching by saving previous states,
jumping to next state and saving things into registers and pushing them onto a stack.
--> So for small functions, the memory utlization is less when compared to the extra time trade off
To eliminate these drawbacks, inline functions are created.
Inline Function:
--> It is expanded in line when the function is called
--> Compiler replaces the function call with the function code present in the inline function
IMP: Inline is a request not a command
--> Compiler gets to decide based on memory usage whether to accept a function as inline or treat it as
a normal function
--> If function has loops, switch, goto, recursion, static variables, etc compiler does not accept it as inline.
*/
int a = 5;
int sq = square(a);
cout << "Square of " << a << " is: " << sq << endl;
}
int square(int x) {
return x * x;
}