Escolar Documentos
Profissional Documentos
Cultura Documentos
An inline function is a function that is expanded in line when it is invoked. That is, the compiler replaces the function call with function definition.
SYNTAX
Inline function-header { Function body; }
Example
inline float square(float x) { return x*x; } void main() { float a,y; a=square(y); } During compilation this code is treated as:
While making inline functions all we need to do is to prefix the keyword inline to function definition.
All inline functions must be defined before they are called. Usually, the functions are made inline when they are small enough to be defined in 1 or 2 lines.
Illustration
#include<iostream.h> #include<conio.h> inline int mult(int x,int y) { return(x*y); } inline int sum(int a,int b) { return(a+b); } int main() { int p,q; cout<<mult(p,q); cout<<sum(p,q); return 0; }
Default arguments
To call a function without specifying its arguments. In such cases, function assigns a default value to the parameter which does not have a matching arguments in the function call. Default values are specified when function is declared.
NOTE: only trailing arguments can have default values. We must add default values from right to left. We can not provide default value to an argument in the middle of argument list. int sum(int i, int j=5, int k=9); //Right int sum(int i=5, int j); //wrong int sum(int i=0, int j; int k=5); //wrong
Advantages
We can use default arguments to add new parameters to existing functions. Default arguments can be used to combine similar functions into one.