Monday 1 October 2012

Functions in ‘C’

Function : A function is a  self-contained block of program  that performs a particular task. A function definition consists of main parts Function  Header Line, The Argument Declarations and Body  of  the Function.


The general form of ‘C’ function  :

Data type  name (argument list)
argument list declarations;
{
local variable declaration
executable statements 1;
executable statements 2;
            |
|
return (expression);
                        }

Where data type represents the data type of the value,  which is  returned  and name represents the function name.  The  formal argument list allow information to be transferred from the calling portion  of the program (argument  parameters) to  the   function. They  are also known as formal parameters. In  argument  declarations, All formal arguments must be declared at this point in  the function.  A  function can return only one  value to  the  calling portion of the program via return statement.

Ex :                 #include <stdio.h>
main()
log int fact();
scanf("%d\n",n);
fact(n);
printf("the fact value = %d\n",fact(n));
long int fact(x)
int x;
int i;
for(i=x-1;i<=1;i--)
x=x*i;
return(x);

 Here, the value of variable 'n'(actual argument) is passed to the variable  x(formal  argument). The function return  the  factorial value of  the  n through to the value of 'x' by return statement.

Category of functions or Types of functions : A  function , depending on whether arguments are present or  not  and whether a value is returned or not , may belong to one of  the following categories.



Type 1:  Functions with no arguments and no return values.

Type 2: Functions with arguments and no return values.

Type 3 : Functions with arguments and return values.

No arguments and no return values : When  a function has no arguments ,it does not  receive any data form the calling function.  Similarly , When it does  not return  a value , the calling function does not receive  any  data from the called function. There is no transfer between the calling function and the called function.


Ex :                 main( )
{
fact( )
}
fact( )
{
int n,i,f=1;
printf("enter the value of n \n :");
scanf("%d\n",&n);
for(i=1;i<=n;++i)
f=f*i;
printf("the factorial of n = %d ",f);
                        }

Arguments but no return values : The  nature   of  data  communication  between  the   calling function  and  the called function with arguments  but  no  return value.  Therefore  this mechanism is also known as  one  way  data communication. When a function call is made , only a copy of the values  of actual  arguments is passed into the called function. What  occurs inside  the function will have no effect on the variables used  in the actual argument list.


Ex :

 main()
{
 int n,fact();
printf("enter the value of the n=');
scanf("%d",&n);
fact(n)
}
fact(x)
int x;
{
int i;
for(i=x-1;i<=1;i--)
{
x=x*i;
}
printf("the factorial of n = %d ",x);
                        }

Arguments with return values : When  in  the calling function to read  data  from  the terminal and pass in on to the called function, the calling  function  inturn  receives  the output from  called  function  through return statement. Thus, we can avoid the I/O operations in  called function.  This method of programming is always wiser. Illustrate  the  use of two-way data communications  between  the calling and the called function.


Ex :
main( )
{
long int n,fact( );
printf("enter the value of the n=');
scanf("%d",&n);
printf("the factorial of n=%d",fact(n));
}
long int fact(x)
int x;
{
int i;
for(i=x-1;i<=1;i--)
{
x=x*i;
}
return(x);
                        }

Passing Arguments to a function(parameters passing techniques) : Actual arguments and formal arguments are used to exchange the data between the calling function and called function.

There are two types of arguments passing techniques that are

Call by value(Value parameters) : When  a function call is made, only a copy of the values  of actual arguments is passed into the called function is the  values
of  the corresponding formal argument can be altered with  in  the function, but the values of the actual arguments within the  calling  function  will not  change. This procedure  for  passing  the values of an arguments to a function is known as call by value.

Ex :                 change(x)                               
x : integer;                              
{                                             
x:= x+10;                                
return(x); }                             

main( )
            {
            int A=2;
            printf("%d",a);
            change(A);
            printf("%d",a);
            }

In the above program, when the calling function  change in passed the actual parameter value a, in to the called  function argument  x .Therefore, the value of x is a copy of A . i.e.., Both  x and  A refers two different memory location. If  any modifications made to x will not effects the value of A. i.e.., the output of the program will be         2 and 2

Thus call by value provides a mechanism by which  function can return a value in calling function, but there is no effect  on the actual arguments.

Call by reference  (functions and pointers)(variable refer)  : When a function call is made,we pass addresses to a function , the parameters receiving the addresses should be pointers.   The process of calling a function using pointers to pass the addresses of variable is known as call by reference.

Ex :                 main( )
{
int x=20;
change(&x);
printf("%d\n",x);
}
change(p)
int *p;
{
*p=*p+10;
}

When  the function change( ) is called , the address  of  the variable x, not its value , is passed into the function  change( ). In  change()  ,  the  variable p is delclared  as  a pointer  and  therefore p is the address of the variable x.  The statement

*p=*p+10;

i.e..,  add  10  to  the  value stored at  the  address  p.  Since  p represents the address of x, the value of  x is changed from 20 to 30. The output of the program will be 30,not 20. Thus , call  by reference provides a  mechanism  by  which  the function can change the stored values in the calling function.

Functions with arrays : When  an  entire array is passed to a  function  as  an argument,  only the address of the first element of the  array  is passed,  but  not the actual values of the  array   elements.  The function  uses this address for manipulations the array  elements. If any changes to the array elements are reflected in the original array in the calling function. Similarly, We can pass the  address of a variable as an arguments to a function call.

Ex :  The call will pass all the elements contained in the array a of size n.

large(a,n);

The called function is, largest function header might look like,

float largest(array,size)
float array[];
int size;

The  function  largerst is defined to take two  arguments  ,  the array name  and the size of the array to specify  the  number  of elements in the array.

The declaration is as follows.

float array[];

Ex :  A problem of finding the largest value in an array  of  elements.  The program is as follows

main()
{
int largest();
int value[4]={78,89,6,9}
printf("%d\n",largest(value, 4);
}
int largest(a,n);
int a[],n;
{
int i,max;
max=a[0];
for (i=1;i<n; i++)
{
if (max<a[i])
max=a[i];
}
return(max);
                                    }

When the function call largest(value, 4) is made, the values of all elements of the array value are passed to the corresponding elements of array of array a in the called function.  the  largest fuction  finds  the  largest value in the array  and  returns  the result to the main.

Recursion :  Recursion is a process by which  a  function  calls itself repeteatly, until some specified condition has been  satisfies. The  process is used for repetitive computations  in  which each action is stated in terms of a previous result.

Ex :   A function to factorial of n is follows

fact(n);
int n;
{
if (n==1)
return(1);
else
facto = n* fact (n-1);
return(facto);
                        }

Evaluation :  Assume n=3,since the value of n is not 1, the statement facto = n*fact(n-1);  will  be executed with n=3 , ie,  The sequence of  operation can be summarized as follows

facto=3*fact(2)
facto=3*2*fact(1)
facto=3*2*1

No comments:

Post a Comment