Saturday 31 August 2013

Call by value | C Programming Language Tutorial pdf



Call by value :

In this type value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any changes made in the formal arguments does not effect the actual arguments because formal arguments are photocopy of actual arguments. Hence when the function is called by the call by value method, it does not effect the actual contents of the actual arguments. Changes made in the formal arguments are local to the block of called function. Once control returns back to the calling function the changes made vanish.
Ex: program to send values by call by value
main( )
{
int x,y, change (int, int);
clrscr();
printf(“ \n enter values of x & y : “);
scanf(“%d %d “, & x, & y);
change(x,y) ;
printf(“\n In main ( ) x=% d y = % d”, x,y);
return 0;
}
change(int a, int b)
{
k = a; o/p: enter values of x & y : 5 4
a=b; In change (1) x=4 y=5
b=k;
}
In the above program, the variables a and b defined in function definition are known as formal parameters or dummy parameters or place holders. The variables x and y are actual parameters, they specify the values that are passed to the function change x and y are arguments in the function change x and y arguments in the function call.
The number of arguments in the function call and the function declarator must be the same.
The date type of each of the arguments in the function call should be the same as the corresponding parameter in the function declaration statement.
The names of the arguments in the function call and the names of parameters in the function definition can be same or different.

C Provides two mechanisms to pass arguments to a in
- pass arguments by value, and
- pass arguments by address or by pointers
An argument may take the form of a constant, variable, expression, pointer, structure etc.
Category 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.

1.Function with no arguments and no return values:
-when a function has no arguments, it does not receive any data from the calling function
-when a function does not return a value, the calling function does not receive any data from the called function.
-There is no data transfer between the calling function and the called function.

2.Function with arguments but no return values:
- The main function has no control over the way the functions receive input data.
- we shall modify the definitions of both the called functions to include arguments as follow.
void printline(char ch)
void value (float p, float r, int n)
- The arguments ch, p, r and n are called formal arguments
- The calling function can now send values to there arguments using function calls containing appropriate arguments
- For example the function call value (500, 0.12, 5) would send the values 500,0.12 & 5 to the function void value (float p float r, int n) and assigns 500
to p, 0.12 to r and 5 to n values 500, 0.12 and 5 are the actual arguments, which become the values of the formal arguments inside the called function.
The actual and formal arguments should match in number, type and order.
The values of actual arguments are assigned to the formal arguments on a one to one basis, starting with the first argument.

3. Function with arguments with Return values
- The function value in previous program receives data from the calling function through arguments. But does not send back any value.
- Rather, it displays the results of calculations at the terminal.
- We may not always wish to have the result of a function displayed.
- We may use it in the calling function for further processing
- Sometimes, different programs may require different output formats for displays of results.
- There short comings can be overcome by handling over the result of a function to its calling function where the returned value can be used as required by the program
- A self contained and independent function should behave like a 'black box' that receives a predefined form of input and output a desired value
- Such functions will have two way data communications between them
#include < stdio.h >
#include < conio.h>
main()
{ int fact(int k) /* function prototype declaration * /
int n,r,ncr ;
clrscr();
printf( “/ n Enter values of n and r;”);
scanf (“%d %d “. & n, &r:”);
ncr = fact (n) / (fact (r) ;”);
printf (“/n value of ncr - %d”, ncr);
getch( );
}
int fact(int k) /* recursive function to find factorial */
{
if(k = = 1)
return(1);
else
return (k*fact (k+));
}
When the program is executed, the user has to enter value of n and r. The function is called to find factorials of n, r, and (n-r). The value for ncr is obtained and printed as shown below
o/p. Enter values to n and r : 5 : 3
Value of ncr = 10

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved