functions. introduction a sequence of statements that perform some related and useful processing....

23
Functions

Upload: cory-chase

Post on 21-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

Functions

Page 2: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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)

Page 3: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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;

}

Page 4: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 5: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

examplevoid f(int val,int& ref){ val++; ref++;}

void g(){ int i=1; int j=1;

f(i,j);}

Page 6: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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)}

Page 7: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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);}

Page 8: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 9: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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)

Page 10: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 11: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

examplevoid print (int); //print an intvoid 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)}

Page 12: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

example (cont.)

void f(int);

void g()

{

void f(double);

f(1); //calls f(double), no ambiguity

}

Page 13: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 14: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

examplevoid 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*)

}

Page 15: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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*)}

Page 16: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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}

Page 17: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 18: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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

Page 19: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 20: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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}

Page 21: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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.

Page 22: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

example

#define MAC(x,y) x+y

#define SQUARE(a) a*a

int m=10;

int l = SQUARE(m+2);

Page 23: Functions. Introduction A sequence of statements that perform some related and useful processing. Functions have 3 components :- ( ) Note :- Declaration

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);}