Inline function :
One of objectives of using functions is to save some memory space, which becomes apperciable when a function is likely to be called many times. However, every time a function is called , it takes a lot of extra time in executing a series of instructions for tasks such as jumping to the function, saving registers, pushing arguments into the stack and returning to the calling function. When a function is small, a substantial percentage of execution time may be spent in such over heads.
C++ has a different solution to this problem. To eliminate the cost of calls to small function, c++ proposes a new feature calledinline function. An inline function is a function that is expanded in line when it is invoked. That is , the compiler replaces the function call with the corresponding function code. The inline functions are defined as follows :
inline function-header | |
{ | |
function body | |
} |
Example :
inline double cube(double a) | |
{ | |
return(a*a*a) | |
} |
The above inline function can be invoked by statements like :
c= cube(3.5);
d=cube(2.5+4.5);
Usually, the functions are made inline when they are small enough to be defined in one or two lines.
Some of the situations where inline expansion may not work are :
1. For functions returning values, if a loop, a switch, or a goto exists.
2. For functions not returning values, if a return statement exists.
3. If functions containg static variables.
4. if inline functions are recursive.
Block in c++ are ofter nested.
Block 2 is contained in block1. Note that a declaration in an inner block hides a declaration of the same variablein an outer block and therefore, each declaration of x causes it to reger to a different data object. Whitin the inner block, the variable x will refer to the data object declared therein.
In c, global version of a variable cannot be accessed from within the inner block. c++ resolves this problem by introducing a new operator :: scope resolution operator. This can be used to uncober a hidden variable. It takes the following form
:: variable_name
This operator allows access to the global version of a variable.
Memory management operators :
In c++ two unary operators new and delete that perform the task of allocating and freeing the memory.
new :
The new operator can be used to create objects of any type. It takes the following form :
pointer-variable = new data-type ; |
Here, pointer-variable is a pointer of type data-type. The new operator allocates sufficient memory to hold a data object of type data-type and returns the address of the object. The data-type may be any valid data-type. The pointer-variable holds the address of the memory space allocated.
Example :
int *p = new int;
float *q = new float;
*p = 25;
*q = 7.5;
We can also initialize the memory using the new operator. This is done as follows :
pointer-variable = new data-type(value); |
Here , value specifies the initial value.
Examples :
int *p = new int(25);
float *q = new float(7.5);
New can be used to create a memory space for any data type includeing user-defined type such as array, structrues and classes.The general form for a one-dimensional array is :
pointer-variable = new data-type[size]; |
Here, size specifies the number of elements in array.
Example :
Example :
int *p= new int[10];
creates a memory space for an array of 10 integers.
delete:
When a data object is no longer needed, it is destroyed to release the memory space for reuse. The general form of its use is :
delete pointer-variable; |
The pointer-variable is the pointer that points to a data object created with new.
Example :
delete p;
delete q;
If you we want to free a dynamically allocated array, we must use the following form of delete :
delete [size] pointer-variable; |
Call by references :
In traditional c, a function call passes arguments by value. The called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the actual variables in the calling program and can only work on the copies of values.
Provision of the reference variable in c++ permits us to pass parameters to the functions by reference. When we pass arguments by reference, the 'formal' arguments in the called function become aliases to the 'actual' arguments in the calling function. This means that when the function is working its own arguments, it is actually working on the original data. consider the following function.
void swap(int & a, int & b) | |
{ | |
int t=a; | |
a=b; | |
b=t; | |
} |
Now, if m and n are two integer variables, then function call
swap(m,n);
will exchange the values of m and n using their aliases (reference variables) a nd b.
Return by reference :
A function can also return a reference. consider the following function :
int & max(int & x, int & y) | ||
{ | ||
if (x>y) | ||
return x; | ||
else | ||
return y; | ||
} |
Since the return type of max() is int &, the function returns reference to x or y (and not the values).Then a function call such as max(a,b) will yield a reference to either a or b depending on their values. This means that this function call can appear on the left-hand side of an assignment statement. That is, the statement
max(a,b)=-1;
is legal and assigns -1 to a if it is larger, otherwise -1 to b.
Default Arguments :
c++ allows us to call a function without specifying all its arguments. In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call. Default values are specified when the function is declared. The compiler looks at the prototype to see how many arguments a function uses and alerts the program for possible default values. Here is an example of a prototype with default values :
float amount(float principal, int period, float rate=0.15);
The default value is specified in a manner syntactically similar to a variable initialization. The above prototype declares a default value of 0.15 to the argument rate. A subsequent function call like
value = amount(5000,7) // one argument missing
passes the 5000 to principal and 7 to period and then lets the function use default values of 0.15 for rate. The call
value= amount(5000,5,0.12); // no missing argument
passes an explicit value of 0.12 to rate.
One important point to note is that only the trailing arguments can have default arguments can have default values. That is, we must add defaults from right to left. We cannot provide a default value to a particular argument in the middle of an argument list. Default arguments are useful in situations where some arguments always have the same value.