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

Solve-write C program to find biggest number from three number by using nested if statement

          C program to find biggest number from three number                                                by using                                                                 nested if statement Problem Description This program takes the 3 numbers and finds the biggest among all. Problem Solution 1. Take the three numbers as input. 2. Check the first number if it greater than other two. 3. Repeat the step 2 for other two numbers. 4. Print the number which is greater among all and exit. Program codes:- here the program of finding biggest of three number run this in your Linux, DEV C++,turbo c++ #include<stdio.h> main() { int num1,num2,num3,big;   ...

Minterm and maxterm in Digital logic design - Computer Science fundamentals tutorial

Minterm and Maxterm First thing to know before we proceed   towards   what is ‘minterm’ and ‘maxterm’ we have to know the sum of product and product of sum. Sum of product: - The logical sum of two or more logical product term is called sum of products expression. It is basically an OR operation of AND operated variables such as Y = AB+BC+ABC Product of Sum: - The logical product of two or more logical sum term is called product of sums expressions. It is basically an AND operation of OR operated variables such as Y = (A+B).(B+C).(A+B+C) Minterm: - Product term containing all the k variables of the functions is either complimented or uncomplimented form is Minterm. Canonical form of sum of product: - It is defined as the logical sum of all the minterms derived from the rows of a truth table for which value of the function is 1. It is called a minterm canonical form. The canonical sum of product expression can be given in a compact form by lis...

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