Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, April 18, 2020

Function call by value in C

Function Call by value: When we pass a copy of variables as an actual arguments while calling a function then this is known as function call by value.

Function call by value | welcome2protec


When we pass a copy of variables as an actual arguments while calling a function then this is known as function call by value. Before we discuss function call by value, lets understand the technical word that we will use while explaining this.


Arguments or Parameters in C:

Arguments are nothing but variables declared inside the parenthesis () is called arguments (OR Parameters). For example: sum(int a, int b); where variables a and b are the arguments or parameters.


Actual Arguments or Parameters:

The arguments that appear in function calls is called actual arguments (or actual parameters). And these value of an actual parameters is copies into the formal parameters.


Formal Arguments or Parameters:

The arguments that appear in function definition is called formal arguments (or formal parameters).

Remember: we cannot modify the value of the actual parameter by the formal parameter.


For Example: Actual or Formal Arguments

 /*Program two find sum of two integers input given by user*/
 #include <stdio.h>
 #include <conio.h>
 /*Function declaration OR function prototype*/
 int sum(int, int); 
 void main()  
 {  
  int a, b, sum;  
  clrscr();  
  printf("Eneter two numbers: "); 
  scanf("%d%d\n", &a, &b);
  /* Function call by passing value */
  sum = sum(a, b);
  printf("sum = %d", sum); 
  getch();  
 } 
 /* Creating user defined function OR function definition*/ 
 int sum(int n1, int n2)
 {
  int result;
  result = n1 +n2;
  return result;
 }

variables n1 and n2 are the formal parameters (or formal arguments). look at the below statements taken form the above example:

/* Creating user defined function OR function definition*/ 
   int sum(int n1, int n2)

Variable a and b are the actual arguments (or actual parameters). look at the below statements taken form the above example:

/* Function call by passing value */ 
    sum(a, b);

The actual parameters can also be the values. Like sum(10, 20), here 10 and 20 are actual parameters. 


Now Let's back on the topic.

Function call by value in C:

  • When we pass a copy of variables as an actual arguments while calling a function then this is known as function call by value.
  • In call by value, the value of the actual parameters is copied into the formal parameters.
  • In call by value, we can not modify the value of the actual parameter by the formal parameter.
  • In call by value, actual and formal arguments will be created in different memory location.

Example 1: Function call by value

As we know that, in the call by value the actual arguments are copied to the formal arguments, hence any operation performed by function on formal arguments doesn’t affect actual arguments. Lets take an example to understand it better.


  #include <stdio.h> 
  #include <conio.h>
  /* Creating user defined function OR function definition*/ 
  int modification(int num1)
  {
    num1 = num1 + 10;
    return num1;
  }
  void main()  
  {  
   int x = 100, y;  
   clrscr();  
   /* Function call by passing value */
   y = modification(x);
   printf(" Value of x = %d \n", x);
   printf(" Value of y = %d ", y); 
   getch();  
} 
  

Explanation: We passed the variable x while calling the function, but since we are calling the function using call by value method, only the value of x is copied to the formal parameter num1. Thus change made to the num1 doesn’t reflect in the x.

Output

Value of x = 100 
Value of y = 110 
  

Example 2: Swapping numbers using function call by value

 #include <stdio.h>   
 void swap(int , int); //prototype of the function   
 void main()  
 {  
   int num1 = 10;  
   int num2 = 11;   
   printf("Value before swapping: %d, %d\n", num1, num2); 
   swap(num1, num2);  // num1 and num2 are the actual arguments or parameters
   printf("Value after swapping: %d, %d\n", num1, num2);   
 }  
 void swap (int a, int b) // a and b are the formal arguments or parameters
 {  
   int t;   
   t = a;  
   a = b;  
   b = t;  
 }  

Output

Value before swapping: 10, 11 
Value after swapping: 11, 10 
  

Things to remember:

  • In call by value, a copy of the variable is passed whereas in call by reference, a variable itself is passed
  • In call by value, actual and formal arguments will be created in different memory location whereas in call by reference, actual and formal arguments will be created in the same location
  • In the call by value, the actual arguments are copied to the formal parameters. Hence any operation performed by function on formal arguments doesn’t affect actual arguments.
  • The arguments that appear in function calls is called actual arguments (or actual parameters). And the arguments that appear in function definition is called formal arguments (or formal parameters).







Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us