Skip to main content

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 


     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

Popular posts from this blog

Important MCQ of RDBMS( Relational database management system)-FCST

Important MCQ of RDBMS  1. A RDBMS consists a collection of ? a. Tables b. Fields c. Records d. Keys  ANS/- a. table 2. The term attribute refers to a ___________ of a table a. Record b. Tuple c. Column d. Key   ans/- c. Column 3. In relational model, the row of table is known to be ?  a. Relation b. Entity field c. Tuple d. Attribute  ans/- C. Tuple 4. . Address field of a person should not be part of primary key, since it is likely to ? a. Dependent b. Too long c. Changed d. Not changed  ans/- c. Changed 5. The relational model is concerned with ? a. Data structure and Data integrity b. Data Manipulation c. Both a and b d. None of these  ans/- c. Both a and b 6. Which is the false statement from the following ? a. A veiw is a named derived table b. A name relation is variable c. A veiw is a named reation and is virtual d. None of these  ans/- d. None of these 7. The union of primary key...

Short notes on database schema & database independence- computer science fundamentals tutorial

            Database Schema &  Data Independence PDF version of this post also available. to download the click the link given at the end of this post.           Database Schema : -  A database schema   is the skeleton structure that represents the logical view of the entire database. It defines how the data is organized and how relat ion among them are associated.  Database schema  formulates all the constrains that are to be applied on the data. A database schema defines its entities and the relationship among them. It contains descriptive details of the database which can be described by means of schema diagram. A database schema can be divided into two categories.   I.     Physical database schema : - this schema contain to the actual storage of data and its form of storage like files, indices etc. It defines how the data will be stored in a secondary storage. ...

C program to find factorial of number using function - FCST

C program to find factorial of number using function Problem description In this program take number as input & then it show the factorial of given number as output. Problem solution 1.take a number as input. 2.declare the void Fact(int) function globally if you want or declare the function prototype  into the main() function. 3.after taking input call the function into main() . 4.then define the function definition. 5.after taking function definition for loop started in this function definition. 6. In for loop i=1 & fact=1 then fact=1. after this loop is continue and incremented by1 until the n is grater than equal to i. continuity of loop is depend on what number take as input .  Program code: #include<stdio.h> void Fact(int);//function prototype declearation int i,fact=1;//global variable declearation int main()  { int n; printf("\n enter the  number:"); scanf("%d",&n); Fact(n);//function call } //func...