Call by Value and reference

Views:
 
Category: Entertainment
     
 

Presentation Description

c programmin

Comments

Presentation Transcript

Call by Value and Call by Reference: 

Call by Value and Call by Reference

PowerPoint Presentation: 

In C programming language, variables can be referred differently depending on the context. For example, if you are writing a program for a low memory system, you may want to avoid copying larger sized types such as structs and arrays when passing them to functions. On the other hand, with data types like integers, there is no point in passing by reference when a pointer to an integer is the same size in memory as an integer itself.

Now, let us learn how variables can be passed in a C program. : 

Now, let us learn how variables can be passed in a C program.

Pass By Value: 

Pass By Value Passing a variable by value makes a copy of the variable before passing it onto a function. This means that if you try to modify the value inside a function, it will only have the modified value inside that function. One the function returns, the variable you passed it will have the same value it had before you passed it into the function. When a function is called, any arguments that are provided by the caller are simply treated as expressions. The value of each expression has the appropriate conversions applied and is then used to initialize the corresponding formal parameter in the called function, which behaves in exactly the same way as any other local variables in the function .

Example 1: 

E xample 1 #include <stdio.h> #include <stdlib.h> main(){ void changer(int); int i; i = 5; printf("before i=%d\n", i); changer(i); printf("after i=%d\n", i); exit(EXIT_SUCCESS); } void changer(int x){ while(x){ printf("changer: x=%d\n", x); x--; } }

PowerPoint Presentation: 

Example 1: The result of running that is: before i=5 changer: x=5 changer: x=4 changer: x=3 changer: x=2 changer: x=1 after i=5 The function changer uses its formal parameter x as an ordinary variable—which is exactly what it is. Although the value of x is changed, the variable i (in main) is unaffected. That is the whole point—the arguments in C are passed into a function by their value only, no changes made by the function are passed back.

Pass By Reference: 

Pass By Reference There are two instances where a variable is passed by reference: When you modify the value of the passed variable locally and also the value of the variable in the calling function as well. To avoid making a copy of the variable for efficiency reasons. does allow functions to change values in their callers.

PowerPoint Presentation: 

#include <stdio.h> #include <stdlib.h> void printtotal(int total); void addxy(int x, int y, int total); void subxy(int x, int y, int *total); void main() { int x, y, total; x = 10; y = 5; total = 0; printtotal(total); addxy(x, y, total); printtotal(total); subxy(x, y, &total); printtotal(total); getch(); }

PowerPoint Presentation: 

void printtotal ( int total) { printf ("Total in Main: %d\n", total); } void addxy ( int x, int y, int total) { total = x + y; printf ("Total from inside addxy : %d\n", total); } void subxy ( int x, int y, int *total) { *total = x - y; printf ("Total from inside subxy : %d\n", *total); }