What is call by value and call by reference?

1 Answer

Answer :

PASSING PARAMETERS TO FUNCTIONS :
The called function receives the information from the calling function through the parameters. The
variables used while invoking the calling function are called actual parameters and the variables used in
the function header of the called function are called formal parameters.
 C provides two mechanisms to pass parameters to a function.

 1 : Pass by value (OR) Call by value.
 2 : Pass by reference (OR) Call by Reference.
1 : Pass by value (OR) Call by value : When a function is called with actual parameters, the values of actual
parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the values of the actual parameters are not changed. This way of passing parameters is called pass by value or call
by value.
Ex :
 #include<stdio.h>
 #include<conio.h>
 void swap(int a,int j);
 void main()
 {
 int i,j;
 printf("Enter i and j values:");
 scanf("%d%d",&i,&j);
 printf("Before swapping:%d%d\n",i,j);
 swap(i,j);
 printf("After swapping:%d%d\n",i,j);
 getch();
 }
 void swap(int a,int b)
 {
 int temp;
 temp=a;
 a=b;
 b=temp;
 }
2 : Pass by reference (OR) Call by Reference :
In pass by reference, a function is called with addresses of actual parameters. In the function header, the
formal parameters receive the addresses of actual parameters. Now the formal parameters do not contain values,
instead they contain addresses. Any variable if it contains an address, it is called a pointer variable. Using
pointer variables, the values of the actual parameters can be changed. This way of passing parameters is called
call by reference or pass by reference.
Ex : #include<stdio.h>
#include<conio.h>
 void swap(int a,int j);
 void main()
 {
 int i,j;
 printf("Enter i and j values:");
 scanf("%d%d",&i,&j);
 printf("Before swapping:%d%d\n",i,j);
 swap(&i ,&j);
 printf("After swapping:%d%d\n",i,j);
 }
 void swap(int *a,int *b)
 {
 int temp;
 temp=*a;
 *a=*b;
 *b=temp;
 }

Related questions

Description : What is call by reference? (OR) what is pass by reference?

Last Answer : Pass by reference (OR) Call by Reference : In pass by reference, a function is called with addresses of actual parameters. In the function header, the formal parameters receive the addresses of actual parameters. Now the formal parameters do ... a,int *b) { int temp; temp=*a; *a=*b; *b=temp; }

Description : What is call by value? (OR) what is pass by value?

Last Answer : Pass by value (OR) Call by value : When a function is called with actual parameters, the values of actual parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the values of ... void swap(int a,int b) { int temp; temp=a; a=b; b=temp; }

Description : What is calling function? (OR) what is function call?

Last Answer : Function Call : The function can be called by simply using the function name. Syntax : function-name(); function-name(parameters); return value = function-name(parameters); Ex : add(); // function ... ,b); // function with arguments. c=fun(a,b); // function with arguments and return values

Description : Write a C program addition of two matrices?

Last Answer : #include #include void main() { int a[25][25],b[25][25],c[25][25],i,j,m,n; clrscr(); printf("enter the rows and colums of two matrics:\n"); scanf("%d%d",&m,&n); printf("\nenter the elements of A matrics"); for(i=0;i

Description : Write a C program multiplication of two matrices?

Last Answer : // Write a C program Multiplication of Two Matrices. #include #include void main() { int a[25][25],b[25][25],c[25][25],i,j,m,n,k,r,s; clrscr(); printf("enter the rows and colums of A matrics:\n"); ... B matrics:\n"); scanf("%d%d",&r,&s); printf("\nenter the elements of A matrics"); for(i=0;i

Description : Explain category of functions with examples?

Last Answer : CATEGORY OF FUNCTIONS : A function, depending on whether arguments are present or not and whether a value is returned or not. The following are the function prototypes. 1 : Functions with no Parameters and no Return Values. 2 : ... (); } int sum(int a,int b) { int c; c=a+b; return c; }

Description : what is function? Write types of function with example?

Last Answer : Function: Function means, a large program can be divided into a series of individual related programs called modules. These modules are called functions. A function is a program segment that carries out ... efficiency of program. 4 : Function sharing : A function can be shared by many programmers.

Description : How to initialization of array with example?

Last Answer : Initilization of Array : assigning the required information to a variable before processing is called initialization.we can initialize the individual elements of an array. Array elements can be initialized at the time of declaration. Syntax : ... M','P','U','T','E','R'}; char b[] = COMPUTER ;

Description : What is an Array? Write advantages of arrays?

Last Answer : An array is a collection of similar data items. All the data items of an array are stored in continuous memory locations in RAM. The elements of an array are of same data type and each item can be accessed using the same name.

Description : What is the macro function?

Last Answer : Macro Substitution : They are two types of macro substitution. 1 : Macro substitution without arguments. 2 : Macro substitution with arguments. 1 : Macro substitution without arguments : It is a process to ... varn like the formal parameters in a function definition. Ex : #define PROD(x) (x*x)

Description : Write categeory of functions ?

Last Answer : CATEGORY OF FUNCTIONS : A function, depending on whether arguments are present or not and whether a value is returned or not. The following are the function prototypes. 1 : Functions with no ... : Functions with Parameters and no Return Values. 4 : Functions with Parameters and Return Values.

Description : What is return statement in function?

Last Answer : Return Statement : The return statement may or may not send back any values to the calling function(main program). Syntax : return; // does not return any value or return(exp); // the ... function can return some values other than int type, then we must specify the data type to be return.

Description : What are the function variables? (OR) What are the local and global variables?

Last Answer : FUNCTION VARIABLES : They are two types of variables. 1 : Local Variable. 2 : Global Variable. 1 : Local Variable : The local variables are defined within the body of the function or block. These ... variables m' and n' are defined outside the main() function. Hence they are global variables.

Description : What are the function parameters?

Last Answer : PARAMETERS : parameters provides the data communication between the calling function and called function. They are two types of parametes 1 : Actual parameters. 2 : Formal parameters. 1 : Actual Parameters : These ... . . } Where a, b are the Actual Parameters x, y are the Formal Parameters

Description : What is function definition? (OR) what is user defined function? (OR) Write syntax of user defined function?

Last Answer : Function Definition : The program module that is written to achieve a specific task is called function definition. Syntax : type function-name(parameter list) // function header. { declaration of variables; body of function; // ... and b ); scanf( %d%d ,&a,&b); c=a+b; printf( %d ,c); }

Description : How to Decleration of function?

Last Answer : Function Declaration (Function Prototype) : Like variable, all functions in a C program must be declared, before they are invoked. The declaration of each function should end with semicolon. Ths is ... : Parameter list declares the variables that will receive the data sent by the calling program.

Description : Write types of functions?

Last Answer : The functions can be classified into two categories. 1 : Built in Functions (OR) Library Functions 2 : User-defined Functions. 1 : Built-in Functions (OR) Library Functions : The standard C ... increase efficiency of program. 4 : Function sharing : A function can be shared by many programmers.

Description : What is a function? Write advantages of function?

Last Answer : Function: Function means, a large program can be divided into a series of individual related programs called modules. These modules are called functions. A function is a program segment that carries ... efficiency of program. 4 : Function sharing : A function can be shared by many programmers

Description : What is the difference between Call by value and Call by reference?

Last Answer : A: Call By Value : When a function is called by value, then the value of the actual parameter is copied to the formal parameter (i.e. a separate copy is made). Any changes made with the ... with the values at that address of the formal parameter affects (changes) the value of the actual parameter.

Description : Distinguish between call by value and call by reference.

Last Answer : Call by value Call by reference   A copy of actual arguments is passed to respective formal arguments.   The address of actual arguments is passed to formal arguments Actual ... is not reflected in other functions  Changes made in the function is reflected outside also.  

Description : What is function? What is call by value? What is call by reference? What is the difference between them?

Last Answer : Function: A function is a group/set of statements/instruction that performs a specific task. It can be system defined i.e. clrscr(), getch(), or user defined i.e. add(), sub(). ... reflected inside as well as outside the function. Difference between Call by Value and Call by Reference:

Description : Differentiate between call by value and call by reference method.

Last Answer : Call by Value Call by reference In call by value, a copy of actual arguments is passed to respective formal arguments. In call by reference, the location, that is, the address of actual arguments is ... ;a,&b); //function call void swap(int *a,int *b)// function definition { }

Description : Why do we call it "raising awareness" in reference to certain things?

Last Answer : answer:I think of it as raising the topic to get more people talking about it. A conversation starter promoting a good end. People see a ribbon, a bra color et al, and they start talking about it ... that's what they mean by raising awareness- not just of the problem, but of the solutions as well.

Description : A company limited by shares has to call the statutory meeting within a period of not less than one month and not more than six months. This period is counted with reference to which one of ... date of actual receipt of certificate of incorporation (D) The date of actual commencement of business

Last Answer : Answer: The date at which the company is entitled to commence business

Description : Write a program to swap two integer values by using call by reference.

Last Answer : #include #include void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; } void main() { int a,b; couta; coutb; cout