TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Thursday, 14 May 2020

Call by value and Call by reference in C | Call by value and Call by reference in C In Hindi | Call by value and Call by reference in C Program

Call by value and Call by reference in C


There are two methods to pass the data into the function in C language.

     1.    Call by value

     2.    Call by reference

 

image of call by value and call by reference in c

Fig: Call by value and call by reference


1.    Call by value in C

In call by value method, the value of the actual parameters is copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method.

In call by value method, we can not modify the value of the actual parameter by the formal parameter.

In call by value, different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter.

The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition.

 

Program

#include<stdio.h>  

void change(int num) {    

    printf("Before adding value inside function num=%d \n",num);    

    num=num+100;    

    printf("After adding value inside function num=%d \n", num);    

}    

int main() {    

    int x=100;    

    printf("Before function call x=%d \n", x);    

    change(x);//passing value in function    

    printf("After function call x=%d \n", x);    

return 0;  

}    

 

Output:

Before function call x=100

Before adding value inside function num=100

After adding value inside function num=200

After function call x=100

 

2.    Call by reference in C


In call by reference, the address of the variable is passed into the function call as the actual parameter.

The value of the actual parameters can be modified by changing the formal parameters since the address of the actual parameters is passed.

In call by reference, the memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.

   Program

     #include<stdio.h>  

    void change(int *num) {    

    printf("Before adding value inside function num=%d \n",*num);    

    (*num) += 100;    

    printf("After adding value inside function num=%d \n", *num);    

}        

    int main() {    

    int x=100;    

    printf("Before function call x=%d \n", x);    

    change(&x);//passing reference in function    

    printf("After function call x=%d \n", x);     

    return 0;  

     }        

 

Output:

 

Before function call x=100

Before adding value inside function num=100

After adding value inside function num=200

After function call x=200

 


No comments:

Post a Comment