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

type casting in C. - computer science fundamentals tutorial

Typecasting in C Typecasting is also known as "forced conversion". It refers to changing variable one data type to another data type.          Typecasting in can be certified into following two types: - 1) Implicit type casting. 2) Explicit type casting. Implicit type casting : - It is also known as "Automatic type conversion". It is done by compiler on its own without any external trigger from user. Generally takes place when in an expression more than one data type is present in such condition. Type conversion take places to avoid data lose. Example : - #include<stdio.h> main() {   char y = 'a';   int b = y; printf("%c",y); printf("%d",b); } Explicit Type casting : - This process also called 'Type casting' and it is user defined . Here the user can type cast the result to make it of particular data type. Example : - #include<stdio.h> main() { int m...

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...

Solve-write C program to find grade of student by using nested else-if statement

C program to find grade of student by using nested else-if statement Problem Description This program take input as your number then show your grade. Problem Solution 1. enter your marks as input. 2. then check your marks with 'If' block's condition. if it satisfied then show your grade as output. 3. if it not satisfied then it checks with all else-if block's condition repeatedly. 4. print the grade according to your given marks as input and exit. Program codes:-   #include<stdio.h> main() { int n; printf("\n enter the marks:"); scanf("%d",&n); if(n>89) printf("O"); else if(n>79) printf("E"); else if(n>69) printf("A"); else if(n>59) printf("B"); else if(n>49) printf("C"); else if(n>39) printf("D"); else printf("F"); } Program explanation:- 1. enter your marks. for example we take 70 . ...