Showing posts with label example of call by value and call by reference. Show all posts
Showing posts with label example of call by value and call by reference. Show all posts

Friday, 9 March 2018

Arguments in c programming

 Function Arguments :-

1.Call by value method:  In call by value method the actual arguments are copied to the formal arguments, so any operation performed by function on arguments doesn’t affect actual parameters.

eg:-


#include<stdio.h>

int addition(int num1, int num2);   /* function declaration */

int main()
{
      int result;   ;       /* local variable definition */ 
    int num1 = 14;
    int num2 = 6;      
   result = addition(num1,num2);   /* calling a function to get addition value */ 


    printf("The addition of two numbers is: %d\n",result);   
    return 0;
}

int addition(int a,int b)    /* function returning the addition of two numbers */
{
    return a + b;

}

Output:The addition of two numbers is:20

2. Call by reference method: In this method, address of actual arguments  is passed to the formal arguments, hence any operation performed on formal parameters will affects the value of actual arguments.

eg:-

#include<stdio.h>

int addition(int *num1, int *num2);  /* function declaration */

int main()
{
   int result ;     /* local variable definition */ 
    int num1 = 14;
    int num2 = 6;
    
      result = addition(&num1,&num2);  /* calling a function to get addition value */  

    printf("The addition of two numbers is: %d\n",result);
    return 0;
}

int addition(int *a,int *b)   /* function returning the addition of two numbers */
{
    return *a + *b;
}

output: The addition of two numbers is:20


For more info click here