Pages

Sunday, 24 August 2014

Functions

Functions
C++ makes prototyping necessary (also known as function declaration)
Return-type function-name (argument-list) ;
Void funct ();    ... is known as ellipses, it represent any no. of parameters
Call by reference: it is established using reference variable but in C we use pointers and indirection.
Return By reference also possible.
Max(a,b)=-1; function call can appear on left hand side of the assignment statement.
Inline functions
They are expanded inline when it is invoked (similar to macros expansion)
It eliminates the overhead involved in function call in case of small functions.
Compiler may ignore a inline declaration if the declaration is too long and treat it as a normal function.
Cases where it may not work are:
Switch, goto, if return statement exists when it doesn’t return anything, if it contains static variable and if its recursive.
Trade-off:  reduces overhead but takes up more memory.
Default Arguments and const arguments
Only trailing elements of arguments can have default values (from right to left) and the function prototype should declare the default values. If const is used then the function cannot change the value of the argument
Function Overloading: First it will see for an exact match, then built-in conversions and then user-defined conversions


No comments:

Post a Comment