Saturday, April 5, 2008

Functions in C

User Defined Functions

What is a Function ??

  • A function is a named code block
  • It is created for a specific purpose
  • The name of a function is a pointer to the starting address of the function code in memory

For e.g : A function may carry out some complex calculations based on a formula and return an end result .You may have to pass some values for this calculation which the function utilizes for calculations

Advantages of Functions :

  • A function can be called upon repeatedly
  • Arguments may be passed to a function
  • A function may return a valueFunctions make a large program modular and easy to manage

Library Functions:

Functions which come with C compiler are known as Standard Library Functions These functions are useful for several commonly carried out tasks

E.g. printf( ) , scanf ( ) , getch( ) , clrscr( ) etc…..

User Defined Functions (UDF) :

We can create functions to suit our needs.

Every function has

  • Name
  • Return data type
  • Arguments list (optional)

Format of a function declaration :-

Return Data Type Function name ( arguments / parameters List ) ;

Eg. void display(void);

Name of the function : display

Return data type void (nothing is returned)

Argument type void (nothing is passed)

float CalculateArea ( float radius) ;

Name of the function : CalculateArea

Return data type float

Argument One argument is passed of type float

float amount( float p , int n , float r ) ;

Function prototype

A Function prototype/declaration informs the model of the function. It consists of:

1) Function name 2) Return data tytpe 3)Argument list

For e.g int f1 ( int ) ;

Function Definition :

We define a code block for a function. This states what is the action to be carried out if the function is called

int f1(int i)

{

-------

}

Function Call

A function is called in another function by actually passing arguments

X = f1(10);

Program:

#include

#include

float area( float radius); /* Function prototype */

float area ( float r) /* Function definition */

{

return ( 3.14 * r * r);

}

main( )

{

float r, a;

clrscr( );

printf(“Enter radius :- “);

scanf(“%f” , &r);

a = area ( r ); /* Function call */

printf( “ AREA = %f Square Units “ , a ) ;

getch( ) ;

return 0 ;

}

Note : A function cannot be defined inside another function in C

Recursive Functions

A recursive function is one that calls itself but which halts at some definite point to avoid infinite recursion. This concept can be explained by reference to the factorial function in mathematics. Given a positive n, the factorial of that number can be defined as 1! = 1

n! = (n) ( ( n – 1 )! )

Suppose we wish to find the factorial of 7. Note that the second line in the first formula makes reference to itself, which is what we mean by recursion. Therefore,

7! = ( 7 ) ( 6 ! )

6 ! = ( 6 ) ( 5 ! ),

7 ! = ( 7 ) ( 6 ) ( 5 ) ( 4 ) ( 3 ) ( 2 ) ( 1 !) but 1 ! = 1

The recursive function factorial calls itself.

long factorial ( int n )

{

if ( n = = 1 )

return 1L ;

else

return ( n * factorial ( n – 1 ) );

}

Program prints the revere of input text data. It uses a recursive function called reverse( )

#include

void reverse ( void );

void reverse( void )

{

char ch;

if ( (ch = getchar( ) ) ! = EOF )

/* Accept a character and push it into stack */

reverse ( ) ; /* recursion ; Function calls itself */

putchar ( ); /* pop out from the stack

}

void main ( )

{

clrscr( );

printf( “ Enter text , press ^z to end : );

reverse (); /* Call reverse ( ) function */

getch();

return 0;

}

Recursive functions look easy to code but involve stack overhead and hence come with performance penalty.

No comments: