C by Value
C 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.
::::::::::::::::::::::
#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();
}
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.
Program Breakdown:
#include <stdio.h>
#include <conio.h>
2. Function declaration:
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:
Explanation
1. Main Function:
2.Swap Function:
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:
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();
}
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:
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:
Explanation
1. Main Function:
2.Swap Function:
Output
If the user inputs 5 and 10 for x and y, respectively, the output will be: