You're online now.

Hurray! you are online now.

Function call by value in c programming language

The call by value technique of passing arguments to a function copies the particular value of an argument into the formal parameter of the function. During this case, changes created to the parameter within the operate don't have any result on the argument.

In call by value the actual value cannot modified by the formal parameters.

In call by value different memory allocated to the formal parameter because formal parameter in the copy of the actual value.

Where the actual argument used in the function call whereas formal parameter used in the definition of the function.

#include<stdio.h>
    
    void func(int x);
    
    int main(){
        int n = 40;
        // Before calling the function the value of n
        printf("Before Calling the Function value of n : %d", n);
        func(n);
        printf("After Calling the Function value of n : %d", n);
        return 0;
    }
    
    void func(int x){
        // Inside the function print the value before perform some operation
        printf("Inside the function the value before perform operation : %d", x);
    
        // Perform some operation
    
        x = x + 30;   // value changed of x
        
        printf("Inside the function the value after perform operation : %d", x);
    }    
    
    /*
    
    Before Calling the Function value of n : 40
    Inside the function the value before perform operation : 40
    Inside the function the value after perform operation : 70
    After Calling the Function value of n : 40
    
    */
// Swap to number using function
    #include<stdio.h>
    
    void swap(int x, int y);
    
    int main(){
        
        int n = 34;
        int m = 20;
        
        // Before Calling function the value of n and m
        printf("Before Calling the function the value of n = %d , m = %d", n, m);
        
        // Calling function
        swap(n, m);
        
        // After Calling function the value of n and m
        printf("\nAfter Calling the function the value of n = %d , m = %d", n, m);
        return 0;
    }
    void swap(int x, int y){
        // Before Swap the value of x and y 
        
        printf("\nInside the function Before Swapping the value of x = %d, y = %d", x, y);
        
        // Swapping 
        
        int temp = x;
        x = y;
        y = temp;
        
        // After swap the value of x and typedef
        
        printf("\nInside the function After Swapping the value of x = %d, y = %d", x, y);
    }
    
    
    /*
    
    Before Calling the function the value of n = 34 , m = 20
    Inside the function Before Swapping the value of x = 34, y = 20
    Inside the function After Swapping the value of x = 20, y = 34
    After Calling the function the value of n = 34 , m = 20
    
    */
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...