← Back to list

Inline Function

When you call a function, it requires extra time for tasks like jumping to the function, saving registers, passing arguments to the stack…

Rudra Nepal · 2024-05-30 06:09 · 0 claps · 2.9 min read
#inline-functions #default-arguments #function-overloading #reference-variable
Open on Medium ↗
Wiki topics: PFI · Personal Finance

Inline Function

When you call a function, it requires extra time for tasks like jumping to the function, saving registers, passing arguments to the stack, and returning to the calling function. These overheads can be significant for small functions.To minimize these overheads, C++ introduces inline functions. An inline function is one where the compiler replaces the function call with the actual function code wherever it’s invoked.To declare a function as inline, you use the inline keyword before its definition. It's important to define inline functions before they're called.

Program to calculate area of rectangle using inline function.

include<iostream>

using namespace std; inline int area(int l,int b){ return (l*b); } int main() { int len,bre,result; cout<<“Enter values of length and breadth”; cin>>len>>bre; result=area(len,bre); cout<<endl<<“The area of rectangle is “<<result; return 0; }

Note: Some of the situations where inline expansion may not work are  . For functions returning values, if a loop, a switch or a goto exists.  . For functions not returning values, if a return statement exists.  . If function contain static variables.  . If inline functions are recursive.

Reference Variable A reference variable provides an alias(alternative name) for a previously defined variable.A reference variable can be created as follows: data-type &reference-name = variable name; eg float total = 100; float &sum = total; // creating reference variable for ‘total’. Example:

include<iostream>

using namespace std; int main() { float total=100; float &sum=total; cout<<”Total=”<<total<<endl; cout<<”Sum=”<<sum<<endl; total=total+100; cout<<”Total=”<<total<<endl; cout<<”Sum=”<<sum<<endl; return 0; }

Output: Total=100 Sum=100 Total=200 Sum=200

In the above example, we are creating a reference variable ‘sum’ for an existing variable ‘total’. Now these can be used interchangeably. Both of these names refer to same data object in memory. If the value is manipulated and changed using one name then it will change for another also. Eg- the statement total = total + 100; will change value of ‘total’ to 200. And it will also change for‘sum’. So the statements cout<<total; cout<<sum; both will print 200. This is because both the variables use same data object in memory.

Default argument In C++, functions can be defined with default arguments, allowing parameters to be assigned default values if no argument is provided for them in the function call. These default values are set when the function is declared, similar to initializing a variable.

If a function call omits an argument corresponding to a parameter with a default value, the default value is used. However, if a value is provided for that parameter in the function call, the default value is ignored, and the provided value is used instead.

  • Default arguments are particularly useful when certain parameters consistently have the same value. For instance, the interest rate in a bank may remain constant for all customers over a specific deposit period.
  • Default values can only be assigned to trailing arguments in a function’s parameter list. Thus, defaults must be added from right to left.
  • It’s not permissible to assign a default value to a parameter in the middle of the argument list.

int add (int a, int b = 5, int c); // illegal int add (int a = 5, int b, int c = 6); // illegal int add (int a int b , int c = 5); // legal int add (int a = 5, int b = 6, int c = 7); // legal

Example:

include<iostream>

include<conio.h>

using namespace std; int add(int a=1, int b=2, int c=3); int main() { int x=5,y=10,z=15; cout<<”Sum=”<<add(x,y,z)<<endl; cout<<”Sum=”<<add(x,y)<<endl; cout<<”Sum=”<<add(x)<<endl; cout<<”Sum=”<<add()<<endl; getch(); return 0; } int add(int a, int b, int c) { return (a+b+c); } Output: Sum=30 Sum =18 Sum =10 Sum =6

Using class write a program that receives inputs principle amount, time and rate.Keeping rate 8% as the default argument, calculate simple interest for three customers.

include<iostream>

using namespace std; class customer { private: float principle,rate,si; int time; public: void setdata(float p,int t,float r=8); void display(); }; void customer::setdata(float p,int t,float r) { principle=p; time=t; rate=r; } void customer::display() { si=(principletimerate)/100; cout<<”Simple Interest=”<<si<<endl; } int main() { float p; int t,i; customer c[3]; for(i=0;i< =3 ;i++) { cout<<”Enter principle and time for customer”<<i+1<<endl; cin>>p>>t; c[i].setdata(p,t); c[i].display(); } return 0; }

WAP in C++ to calculate simple interest from given principal, time and rate. Set the rate to 15 % as default argument when rate is not provided.

include<iostream>

using namespace std; float calculate(float principle,int time,float rate=15); int main( ) { float p,r; int t; char ch; cout<<”Enter principle and time”<<endl; cin>>p>>t; cout<<”Do you want to enter rate?”<<endl; cin>>ch; if(ch==’Y’||ch==’y’) { cout<<”Enter rate”<<endl; cin>>r; cout<<”Interest=”<<calculate(p,t,r)<<endl; } else { cout<<”Interest=”<<calculate(p,t)<<endl; } return 0; } float calculate(float principle,int time,float rate) { return(principletimerate)/100.0; }


메타데이터
post_id
ec40e35a541d
slug
inline-function-ec40e35a541d
url
https://medium.com/@rudra.nepal/inline-function-ec40e35a541d
canonical_url
https://medium.com/@rudra.nepal/inline-function-ec40e35a541d
author_url
https://medium.com/@rudra.nepal
status
ok
fetched_at
2026-08-27 09:18:55