0% found this document useful (0 votes)
6 views

C by Value

The document discusses call by value vs call by reference in C programming. It provides an example program that attempts to swap two numbers using call by value but fails, and another program that successfully swaps the numbers using call by reference by passing the addresses of the variables to the swap function.

Uploaded by

devkotasiddhant7
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C by Value

The document discusses call by value vs call by reference in C programming. It provides an example program that attempts to swap two numbers using call by value but fails, and another program that successfully swaps the numbers using call by reference by passing the addresses of the variables to the swap function.

Uploaded by

devkotasiddhant7
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Call by Value:

In call by value, the values of variables are passed to the function, creating a
copy of the actual parameters in the formal parameters.
When the function is called, a separate copy of the variables is created in
memory, and the value of the original variables is assigned
to these copies. Therefore, changes made within the function do not affect the
original variables. Only one value can be returned.

#include <stdio.h>

void modiValue(int x)
{
x = 100; // This change will not affect the original variable
}

int main()
{
int oriValue = 50;
printf("Before function call: %d\n", oriValue); // Output: 50

modiValue(oriValue);
printf("After function call: %d\n", oriValue); // Output: 50

return 0;
}
In this example, oriValue is passed to the modiValue function. Inside the function,
a separate copy of oriValue is modified,
but the original variable remains unchanged. The change made inside the function
does not reflect in the original variable
after the function call.

::::::::::::::::::::::

WAP to swap two numbers using call by value.

#include<stdio.h>
#include<conio.h>
void swap(int p, int q);
int main()
{
int x, y;
printf("Enter value of two numbers as x and y respectively:");
scanf("%d %d", &x, &y);
swap(x, y);
printf("\n Swapped values of x is %d and value of y is %d", x, y);
getch();
}

void swap(int p, int q)


{
int temp;
temp = q;
q = p;
p = temp;
printf("\n Swapped values of x is %d and value of y is %d", p, q);
}

Explain:
This program attempts to swap two numbers using call by value, but it does not
actually swap the values in the main function because it only swaps the copies of
the values.

Here’s a detailed explanation:

Program Breakdown:

1. Include necessary headers:

#include <stdio.h>
#include <conio.h>

2. Function declaration:

void swap(int p, int q);

3. Main function:

int main() {
int x, y;
printf("Enter value of two numbers as x and y respectively:");
scanf("%d %d", &x, &y);
swap(x, y);
printf("\nSwapped values of x is %d and value of y is %d", x, y);
getch();
}

4. Swap function:

void swap(int p, int q) {


int temp;
temp = q;
q = p;
p = temp;
printf("\nSwapped values of p is %d and value of q is %d", p, q);
}

Explanation

1. Main Function:

-The program starts by declaring two integer variables x and y.


-The user is prompted to enter the values of x and y.
-The scanf function reads these values from the user input.
-The swap function is called with the values of x and y.
-The main function then prints the values of x and y after the swap function call.

2.Swap Function:

-The swap function takes two integers p and q.


-A temporary variable temp is used to facilitate the swap.
-temp stores the value of q.
-q is then set to the value of p.
-Finally, p is set to the value stored in temp.
-The function prints the swapped values of p and q.

Call by Value:
In this program, the swap function operates on the copies of x and y (named p and q
within the function). Therefore, the original values of x and y in the main
function are not modified. The swap function only swaps the copies and prints them,
but these changes are not reflected in the main function.

Output
If the user inputs 5 and 10 for x and y, respectively, the output will be:

Enter value of two numbers as x and y respectively: 5 10

Swapped values of p is 5 and value of q is 10

Swapped values of x is 5 and value of y is 10

Here, the values of x and y remain unchanged in the main function because the swap
function does not affect the original variables; it only affects the copies passed
to it. To actually swap the values of x and y, the program should use call by
reference, as demonstrated in the first example.

===================

Task:
/* Write a program to swap two numbers using call by reference.*/
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b);
int main()
{
int x, y;
printf("Enter 2 variables x and y:");
scanf("%d %d", &x, &y);
swap(&x, &y); /*Here addresses of x and y are passed*/
printf("\n Swapped values of x is %d and value of y is %d",x,y);
getch();
}

void swap(int *a, int *b)


{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
.......

In this program, swapping two numbers is achieved using call by reference. Call by
reference allows a function
to modify the actual variables passed to it, rather than working with copies of the
variables. Here’s a
step-by-step explanation of how the program works:

Program Breakdown
1. Include necessary headers:

#include <stdio.h>
#include <conio.h>

2. Function declaration:

void swap(int *a, int *b);

3. Main function:

int main() {
int x, y;
printf("Enter 2 variables x and y:");
scanf("%d %d", &x, &y);
swap(&x, &y); /* Here addresses of x and y are passed */
printf("\nSwapped values of x is %d and value of y is %d", x, y);
getch();
}

4.Swap function:

void swap(int *a, int *b) {


int temp;
temp = *a;
*a = *b;
*b = temp;
}

Explanation
1. Main Function:

- The program starts by declaring two integer variables x and y.


- The user is prompted to enter the values of x and y.
- The scanf function reads these values from the user input.
- The swap function is called with the addresses of x and y (&x and &y), which
means swap receives pointers to these variables.

2.Swap Function:

-The swap function takes two integer pointers *a and *b.


- A temporary variable temp is used to facilitate the swap.
- temp stores the value pointed to by a (which is x).
- The value pointed to by a (*a) is then set to the value pointed to by b (which is
y).
- Finally, the value pointed to by b (*b) is set to the value stored in temp
(original value of x).
Since the swap function operates on the memory addresses of x and y, it effectively
swaps their values. When control returns to the main function, the swapped values
of x and y are printed.

Output
If the user inputs 5 and 10 for x and y, respectively, the output will be:

Swapped values of x is 10 and value of y is 5


This demonstrates the effectiveness of using call by reference to modify the actual
values of variables within a function.

You might also like