Explanation
The program demonstrates that swapping two numbers using call by value does not work. The values of the variables in the main function remain unchanged after attempting to swap them in a separate function.
Header
The code begins with including the standard input-output header file using #include <stdio.h>.
Function Declaration
The program declares a function swap to attempt to swap two integers:
void swap(int x, int y);: Declares the function with two integer parametersxandy.
Function Definition
The program defines the swap function:
int temp = x;: Stores the value ofxin a temporary variabletemp.x = y;: Assigns the value ofytox.y = temp;: Assigns the value oftemp(original value ofx) toy.printf("Inside swap function: x = %d, y = %d\n", x, y);: Prints the swapped values ofxandyinside theswapfunction.
Variable Declaration
The main() function is defined, and two variables are declared:
int a, b;: Variables to hold the values entered by the user.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the values of a and b:
printf("Enter the value for a and b: ");: Prompts the user to enter the values ofaandb.scanf("%d %d", &a, &b);: Reads the values entered by the user and stores them inaandb.
Before Swap
The program prints the values of a and b before attempting to swap them:
printf("Before swap attempt in main: a = %d, b = %d\n", a, b);: Prints the values ofaandbbefore the swap attempt.
Swap Attempt
The program calls the swap function to attempt to swap the values of a and b:
swap(a, b);: Calls theswapfunction withaandbas arguments.
After Swap
The program prints the values of a and b after attempting to swap them:
printf("After swap attempt in main: a = %d, b = %d\n", a, b);: Prints the values ofaandbafter the swap attempt, showing that they remain unchanged.
Program End
The program ends with return 0; to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
// Swap function that attempts to swap using call by value
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
printf("Inside swap function: x = %d, y = %d\n", x, y); // This will show swapped values
}
int main() {
printf("\n\n Program for demonstrating that swapping of two numbers using call by value method does not work. \n\n");
int a, b;
printf("Enter the value for a and b: ");
scanf("%d %d", &a, &b);
printf("Before swap attempt in main: a = %d, b = %d\n", a, b);
swap(a, b); // Trying to swap using call by value
printf("After swap attempt in main: a = %d, b = %d\n", a, b); // The values remain unchanged
return 0;
}
Output
Program for swapping of two numbers entered by the user through call by value method.
Enter the value for a and b: 12 43
Before: a = 12, b = 43
After: a = 43, b = 12