Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

FE-AI&DS {F2}

Name: Ratish Patil


Roll no: 41-123A8042

Experiment No: 09 Call by Value &


Call by reference.

Aim:
To demonstrate the use of call by value in C programming.

Resources: Windows 10, Dev C


Theory:
In C programming, when arguments are passed to a function by value, copies of the actual
arguments are passed to the formal parameters of the function. Therefore, any changes made to the
formal parameters inside the function do not affect the actual arguments outside the function.

Algorithm:
1. Start

2. Declare variables `a` and `b`.

3. Assign values to `a` and `b`.

4. Print the values of `a` and `b` before swapping.

5. Call the `swap` function with `a` and `b` as arguments.

6. Inside the `swap` function:

a. Declare a temporary variable `temp`.

b. Assign the value of `x` to `temp`.

c. Assign the value of `y` to `x`.

d. Assign the value of `temp` to `y`.

7. Return from the `swap` function.

8. display the values of `a` and `b` after swapping.

9. stop.
Flowchart:

Start

Read a and b

Swap (a,b)
Swap (int x, int y)

Temp = x
Stop
X=y
y = temp

Print x and y

Stop
Program:
#include<stdio.h>
int swap(int x,int y);
int main()
{
int a,b;
printf("Enter a and b:\n");
scanf("%d\n%d",&a,&b);
swap(a,b);
printf("\nCaller Value: a=%d, b=%d\n",a,b);

}
int swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("After swapping: x=%d, y=%d",x,y);
}

Output:

Conclusion:
In this program, we observed that even though the `swap` function is called with the arguments `a`
and `b`, the values of `a` and `b` remain unchanged after the function call. This confirms that C uses
call by value parameter passing mechanism, where copies of actual parameters are passed to the
function. Any changes made to the formal parameters inside the function do not affect the actual
parameters outside the function.
Call by reference

Aim:
To demonstrate the use of call by reference in C programming.

Resources: Windows 10, Dev C


Theory:
Call by reference allows passing arguments to functions by providing their memory addresses,
enabling direct modification of the original values.

Algorithm:
1.Start

2. Declare and define a function that takes arguments by reference.

3. Inside the function, use pointers to access and modify the original values.

4. Call the function with appropriate arguments, passing them by reference.

5.stop

Program:
#include<stdio.h>
int swap( int *x, int *y);
int main ()
{
int a,b ;
printf("Enter the value of a and b:\n");
scanf("%d\n%d",&a,&b);

printf("After swapping, a=%d and b=%d.",a,b);


}
int swap (int *x,int *y)
{
int temp ;
temp = *x;
*x = *y;
*y = temp;
printf("After swapping, x=%d and y =%d",x,y);
}

Output:

Conclusion:
Call by reference provides an efficient method for passing arguments to functions, allowing direct
modification of original values without creating copies. This helps in optimizing memory usage and
enhancing code readability and maintainability.

You might also like