The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
There are two different way a function can be accessed. Eitheir by passing arguments (parameters) to a function or by passing nothing to a function. Function which doesn't accept any parameters are mostly void type.
Accessing function using parameters are of two types. They are
Before getting into the play of call by value and call by reference, one need to know what is actual parametes and formal parameters.
Actual parameters are specified in the function call. In simple terms, value or reference passing at the time of function call.
Formal parameters are specified in the function declaration and function definition. In simple terms, variables with datatype at the function declaration and definition.
Let us write a c program to demonstrate how to use call by value?
#include <stdio.h> int pass(int, int); //function declaration int main() { //main function definition int a = 5, b = 10; printf("The values of a and b in main function before function call : %d %d ", a, b); pass(a, b); //function call with passing arguments as values printf("\nThe values of a and b in main function after function call : %d %d ", a, b); } // function definition int pass(int a, int b) { a = 10; b = 5; //This change will not affect the actual parameters printf("\nThe values of a and b in called function : %d %d ", a, b); }
The program illustrates that, the actual parameters are passed as copy of original parameters to the called function. So, the changes of values inside the called function does not affect the parameters of calling function.
Let us write a c program to demonstrate how to use call by value?
#include <stdio.h> int pass(int*, int*); //function declaration int main() { //main function definition int a = 5, b = 10; printf("The values of a and b in main function before function call : %d %d ", a, b); pass(&a, &b); //function call with passing arguments as addresses printf("\nThe values of a and b in main function after function call : %d %d ", a, b); } // function definition int pass(int * a, int *b) { *a = 10; *b = 5; //This change will affect the actual parameters printf("\nThe values of a and b in called function : %d %d ",*a, *b); }
The program illustrates that, changes made in called function affect the parameters inside the main function. So, the changes made in the parameters are permanent.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019