call by value and call by reference in C also difference between them - Computer science fundamental tutorial
Pointer
A pointer
is a variable like name with points which represents storage location in
memory(RAM).
Address operator(&) : - the symbol ‘&’ is an address
operator which is used to access address of a variable and assign it to it to a
pointer to initialize it.
Indirection operator(*) : - the symbol’*’ is an
indirection operator which is access the value of variable through a pointer.
Pointer
as function argument: -
A pointer
as an argument in function declaration when a function with a pointer argument
is called the calling program pass the address (not the value) of a variable to
the argument.
In C
language a function can be called by the calling programming in two ways. There
are described briefly below
1.Call by value: -when a function is called by the calling
program , the values to the argument in the function are supplied by the
calling program. The value supplied can
be used inside the function. Any alteration to the value inside function is not
excepted in the calling program but the change is locally available in the
function. The method is referred as calling a function by value.
Example: -
#include<stdio.h>
main()
{
int a=25, b=10;
void add (int x, int y);//function prototype declaration
printf(“\n before function call a= %d and b=%d”,a,b);
add(a,b)//function call
printf (“\n after function call a=%d and b= %d”,a,b);
}
void add(int x,int y)//function
definition
{
x =x+10;
y=y+10;
printf(“\n inside function a=%d and b=%d”,x,y);
}
Output
before function call a= 25 and b=10
inside function a=35 and b=20
after function call a=25 and b=
10
The book starts with an introduction to C programming and then delves into an in-depth analysis of various constructs of C. The key topics include iterative and decision-control statements, functions, arrays, strings, pointers, structures and unions, file management, and pre-processor directives. It deals separately with the fundamental concepts of various data structures such as linked lists, stacks, queues, trees, and graphs. The book provides numerous case studies linked to the concepts explained in the text.
With its highly detailed pedagogy entailing examples, figures, algorithms, programming tips, and exercises, the book will serve as an ideal resource for students to master and fine-tune the art of writing efficient C programs. To buy this C Programming book by Reema Thareja Go to Amazon
With its highly detailed pedagogy entailing examples, figures, algorithms, programming tips, and exercises, the book will serve as an ideal resource for students to master and fine-tune the art of writing efficient C programs. To buy this C Programming book by Reema Thareja Go to Amazon
2. Call by reference: - A function can be declared with
pointer as its argument such as function are called by the calling program with
the address of variable as argument from it. Address of the variables are substituted
to the pointer and alteration to its value inside the function is automatically
carried out in that location . the change is indirectly made and is excepted by
the calling program. This method is referred as calling a function by
reference.
Example: -
#include<stdio.h>
main()
{
int a=25, b=10;
void add(int *x,int *y);
printf("\n before function
call a=%d and b=%d",a,b);
add(&a,&b);
printf("\n after function
call a=%d and b=%d",a,b);
}
void add(int *x,int *y)
{
*x=*x+10;
*y=*y+10;
printf("\n inside a=%d and
b=%d",*x,*y);
}
Output
before function call a=25 and b=10
inside a=35 and b=20
after
function call a=35 and b=20
comparison
between call by value and call by reference
Call by value
|
Call by reference
|
1) this is a
usual method to call a function in which only the value of variable is passed
as an argument.
|
1) In this method the address of the variable
passed as an argument.
|
2) Any
alteration in the value of the argument passed is local to the function and
is not accepted in the calling program.
|
2) Any alteration in the value of argument
passed is excepted in the calling program.
|
3) Memory
occupied by formal and actual argument is different.
|
3) Memory location occupied by the formal and
actual argument is same and there is a saving memory location.
|
4) Since a
new location is created by this method is slow.
|
4) Since the existing memory location is used
through its address by this method is fast.
|
5) There is
no possibility of wrong data manipulation. Since argument directly used in an
expression.
|
5) There is a possibility of wrong data
manipulation address are used in an expression. A good skill of programming
is required here.
|
Comments
Post a Comment
please subscribe my blog and let me suggest how I improve this site