C Programming For Beginners And Advanced - Programming In C: C Programming A Modern Approach - C Programming Book For Beginners To Advanced by Patil Kunal

C Programming For Beginners And Advanced - Programming In C: C Programming A Modern Approach - C Programming Book For Beginners To Advanced by Patil Kunal

Author:Patil, Kunal [Patil, Kunal]
Language: eng
Format: epub, azw3, pdf
Publisher: UNKNOWN
Published: 2021-05-07T00:00:00+00:00


The called function receives the information from the calling function through the parameters. The variables used while invoking the calling function are called actual parameters and the variables used in the function header of the called function are called formal parameters.

C provides two mechanisms to pass parameters to a function.

1 : Pass by value (OR) Call by value.

2 : Pass by reference (OR) Call by Reference.

1 : Pass by value (OR) Call by value :

In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main().

Or

When a function is called with actual parameters, the values of actual parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the values of the actual parameters are not changed. This way of passing parameters is called pass by value or call by value.

Ex :

#include<stdio.h>

#include<conio.h>

void swap(int ,int );

void main() {

int i,j;

printf("Enter i and j values:");

scanf("%d%d",&i,&j);

printf("Before swapping:%d%d
",i,j);

swap(i,j);

printf("After swapping:%d%d
",i,j); getch();

}

void swap(int a,int b)

{

int temp;

temp=a;

a=b;

b=temp;

}

Output

Enter i and j values: 10 20 Before swapping: 10 20

After swapping: 10 20

2 : Pass by reference (OR) Call by Reference : In pass by reference, a function is called with addresses of actual parameters. In the function header, the formal parameters receive the addresses of actual parameters. Now the formal parameters do not contain values, instead they contain addresses. Any variable if it contains an address, it is called a pointer variable. Using pointer variables, the values of the actual parameters can be changed. This way of passing parameters is called call by reference or pass by reference.

Ex : #include<stdio.h>

#include<conio.h>

void swap(int *,int *);

void main()

{

int i,j;

printf("Enter i and j values:");

scanf("%d%d",&i,&j);

printf("Before swapping:%d%d
",i,j);

swap(&i ,&j);

printf("After swapping:%d%d
",i,j);

}

void swap(int *a,int *b)

{

int temp;

temp=*a;

*a=*b;

*b=temp;

}

Output Enter i and j values: 10 20

Before swapping:10 20

After swapping: 20 10

Differnce between Call by value and Call by reference

Call by value Call by Reference

1 : When a function is called the values of variables are passed

1 : When a function is called the address of variables are passed.

2 : Change of formal parameters in the function will not affect the actual parameters in the calling function. 2 : The actual parameters are changed since the formal parameters indirectly manipulate the actual parametes.

3 : Execution is slower since all the values have to be copied into formal parameters. 3 : Execution is faster since only address are copied.

1 : Functions with no Parameters and no Return Values.

2 : Functions with no Parameters and Return Values.

3 : Functions with Parameters and no Return Values.

4 : Functions with Parameters and Return Values.

1 : Functions with no Parameters and no Return Values : 1 : In this category, there is no data transfer between the calling function and called function.

2 : But there is flow of control from calling function to the called function.

3 : When no parameters are there , the function cannot receive any value from the calling function.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.