230 likes | 318 Views
Functions. Introduction. A sequence of statements that perform some related and useful processing. Functions have 3 components :- <Return Type> <Function-Name> (<argument-list>) Note :- Declaration does not allocate any space to the argument list and thus, the argument names are ignored.
E N D
Introduction • A sequence of statements that perform some related and useful processing. • Functions have 3 components :- <Return Type> <Function-Name> (<argument-list>) Note :- Declaration does not allocate any space to the argument list and thus, the argument names are ignored. Example) char* strcpy(char* to,char* from)
Function Definition • Actual body of code for the function. • Names all or some of the function parameters. • It is possible to have unnamed parameters for future use. Example) void Foo(int N,char C,float) { if(N>30 && N<100) N=N-30; if(C>='a' && C<='z') C=C-30; }
Argument Passing • A store is set aside for the formal arguments and each formal argument’s memory location is initialized by the actual argument value. • Type-Checking is performed and all promotions,standard conversions and user-defined type-conversions are performed. • Local copy vs. original copy.
example void f(int val,int& ref) { val++; ref++; } void g() { int i=1; int j=1; f(i,j); }
const and non-const arguments • const arguments :- Safeguards programs from modifying original parameter incase of pass-by-reference. Example) ex.1) int strcmp(const char*,const char*); //compares 2 strings.Care is taken to safeguard from accidental change to the original strings ex.2) float fsqrt(const float& rr); void g(double d) { float r=fsqrt(2.0f); //pass by ref to temp holding 2.0f r=fsqrt(r); //pass reference to r r=fsqrt(d); //pass reference to temp holding float(d) }
Array Arguments • Argument of array type (i.e. T[]) is intrinsically converted to T* before passing to a function. • Thus, an array CANNOT be passed by value. • Use const modifier is necessary. Example) void Foo1(int* A); void Foo2(int[] A); void g() { int A[]={2,3,4,5}; Foo1(A); Foo2(A); }
Return Value • Return type must be specified, if none then specify as void. • Recursion. • A return statement is considered to initialize an unnamed, temporary variable of the return type. • Type checking for return values is done. • Do not attempt to return references to local function variables.
example ex.1) int fac(int n) { if(n>1) return n * fac(n-1); return 1; } ex.2) double f(){ ....; return 1;} //1 is implicitly converted to double(1)
Function Overloading • Functions that have same “Function Name” but different argument-lists. • Basic intention is to allow functions to perform semantically similar actions n different contexts. • Return types are not taken into account. • Functions in different scopes do not overload.
example void print (int); //print an int void print(const char*); //print a string float sqrt(float); double sqrt(double); void f() { float a=9.3f; double b=34.55; a=sqrt(a); //calls sqrt(float) b=sqrt(b); //calls sqrt(double) }
example (cont.) void f(int); void g() { void f(double); f(1); //calls f(double), no ambiguity }
Overloaded Function Resolution • Rules to find the best-match between the type of passed argument and type of formal argument. • Rules :- • Exact Match; trivial or no conversions (array name to pointer, function name to pointer and T to const T) • Match using promotions; integral promotions, float to double, double to long double. • Match using standard conversion; (int to double, double to int) • Match using user-defined conversions. • Match using ellipses.
example void print(int); void print(const char*); void print(double); void print(char); void h(char c,int i,short s,float f) { print(c); //exact match: invoke print(char) print(i); //exact match: invoke print(int) print(c); //integral promotion match: invoke print(int) print('a'); //exact match: invoke print(char) print(49); //exact match: invoke print(int) print(0); //exact match: invoke print(int) print("a"); //exact match: invoke print(const char*) }
Manual Ambiguity Resolution • Too few or too many overloaded functions • Try to analyze and resolve ambiguities manually. • Try to use non-overloaded functions. void f1(char); void f1(long); void f2(char*); void f2(int*); void k(int i) { f1(i); //ambiguos : f1(char) or f1(long) f2(0); //ambiguos : f2(char*) or f2(int*) }
Resolution of Multiple Arguments • A function that is the best match for one argument and a better than or equal match for all other arguments is called. int pow(int,int); int pow(double,double); void Foo(double,float); void Foo(int,int); void k() { int r=pow(2,2); int r=pow(2.0,2.0); int r=pow(2.0,2); //error Foo(2.0,2); //Foo(double,float is called }
Default Argument • Allows certain formal arguments to be initialized with some default value incase the call does not explicitly pass values for every argument. • Aimed at having functions that have a short and simple form without overloading. • Default arguments are type-checked at the time of function declaration and evaluated at the time of function call. • Only trailing arguments can be made default.
example • void print(int value,int base=10); • void f() • { • print(31); • print(31,10); • print(31,16); • print(31,2); • } • Output :: 31 31 1f 11111 • int f(int,int=0;char* =0); //ok.note the space between * and = • int f(int=0,int=0;char*); //error • int f(int=0,int;char* =0); //error
Unspecified Arguments • For those functions for which all argument type may not be specified. • The compiler has no information to process the unknown portion of the argument list. Completely programmer dependent. • <cstdarg> library is used.
void error(int severity...) //"severity followed by zero-terminated list of char*'s { va_list ap; //container for the argument list va_start(ap,severity); //load the argument list for(;;) { char* p=va_arg(ap,char*); if(p==0) break; cerr<<p<<' '; } va_end(ap); //cleanup }
Macros • #define preprocessor directive to produce text replacement. • Processed by the preprocessor and no participation from the compiler. • No C++ type check or scope rules. • Overloading and recursion are not allowed. • Use inline functions instead.
example #define MAC(x,y) x+y #define SQUARE(a) a*a int m=10; int l = SQUARE(m+2);
Inline Functions • The “inline” keyword is a hint to the compiler that it should replace the function call by function code. • No guarantee that inline behavior will be followed. • inline functions behave like normal functions other than their call. inline int Foo(int N) { return (N*N)/(N-1); }