1 / 8

C Parameter Passing

C Parameter Passing. Passing Argument to Function. In C Programming we have different ways of parameter passing schemes. Function  is good programming style in which we can write reusable code that can be called whenever require.

tbreeden
Download Presentation

C Parameter Passing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Parameter Passing

  2. Passing Argument to Function • In C Programming we have different ways of parameter passing schemes. • Function is good programming style in which we can write reusable code that can be called whenever require. • Whenever we call a function then sequence of executable statements gets executed. We can pass some of the information to the function for processing called argument.

  3. There are Two Ways of Passing Argument to Function in C Language : • Call by Reference or pass by reference • Call by Value or pass by value

  4. Call by Value • While Passing Parameters using call by value xerox copy of original parameter is created and passed to the called function. • Any update made inside method will not affect the original value of variable in calling function. . • As their scope is limited to only function so they cannot alter the values inside main function.

  5.  Call by reference or address • While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function. • Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.

  6. Summary

  7. Call by value Program • void swapByValue(int, int); /* Prototype */ • int main() /* Main function */ • { • int n1 = 10, n2 = 20; • /* actual arguments will be as it is */ • swapByValue(n1, n2); • printf("n1: %d, n2: %d\n", n1, n2); • } • void swapByValue(int a, int b) • { • int t; • t = a; • a = b; • b = t; • }

  8. Call by reference program • void swapByReference(int*, int*); /* Prototype */ • int main() /* Main function */ • { • int n1 = 10, n2 = 20; • /* actual arguments will be altered */ • swapByReference(&n1, &n2); • printf("n1: %d, n2: %d\n", n1, n2); • } • void swapByReference(int *a, int *b) • { • int t; • t = *a; *a = *b; *b = t; • }

More Related