#include <stdio.h>
// Call by Value - receives copy of arguments
void swapByValue(int a, int b) {
int temp;
printf("\nInside swapByValue (before swap):\n");
printf("a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("Inside swapByValue (after swap):\n");
printf("a = %d, b = %d\n", a, b);
}
// Call by Reference - receives addresses of arguments
void swapByReference(int *a, int *b) {
int temp;
printf("\nInside swapByReference (before swap):\n");
printf("*a = %d, *b = %d\n", *a, *b);
temp = *a;
*a = *b;
*b = temp;
printf("Inside swapByReference (after swap):\n");
printf("*a = %d, *b = %d\n", *a, *b);
}
// Call by Value - modifying the parameter
void incrementByValue(int num) {
printf("\nInside incrementByValue (before):\n");
printf("num = %d\n", num);
num = num + 10;
printf("Inside incrementByValue (after):\n");
printf("num = %d\n", num);
}
// Call by Reference - modifying through pointer
void incrementByReference(int *num) {
printf("\nInside incrementByReference (before):\n");
printf("*num = %d\n", *num);
*num = *num + 10;
printf("Inside incrementByReference (after):\n");
printf("*num = %d\n", *num);
}
// Call by Value - calculating area (returns value)
int calculateArea(int length, int width) {
return length * width;
}
// Call by Reference - calculating area (modifies through pointer)
void calculateAreaByRef(int length, int width, int *area) {
*area = length * width;
}
int main() {
printf("========== CALL BY VALUE vs CALL BY REFERENCE ==========\n");
// Example 1: Swap using Call by Value
printf("\n***** EXAMPLE 1: SWAP FUNCTION *****\n");
int x = 10, y = 20;
printf("\n--- Call by Value ---");
printf("\nBefore calling swapByValue: x = %d, y = %d\n", x, y);
swapByValue(x, y);
printf("After calling swapByValue: x = %d, y = %d\n", x, y);
printf("Notice: Original values NOT changed!\n");
printf("\n--- Call by Reference ---");
printf("\nBefore calling swapByReference: x = %d, y = %d\n", x, y);
swapByReference(&x, &y);
printf("After calling swapByReference: x = %d, y = %d\n", x, y);
printf("Notice: Original values ARE changed!\n");
// Example 2: Increment using both methods
printf("\n\n***** EXAMPLE 2: INCREMENT FUNCTION *****\n");
int num1 = 50, num2 = 50;
printf("\n--- Call by Value ---");
printf("\nBefore calling incrementByValue: num1 = %d\n", num1);
incrementByValue(num1);
printf("After calling incrementByValue: num1 = %d\n", num1);
printf("\n--- Call by Reference ---");
printf("\nBefore calling incrementByReference: num2 = %d\n", num2);
incrementByReference(&num2);
printf("After calling incrementByReference: num2 = %d\n", num2);
// Example 3: Calculate area
printf("\n\n***** EXAMPLE 3: CALCULATE AREA *****\n");
int length = 5, width = 10;
printf("\n--- Call by Value (return value) ---\n");
int area1 = calculateArea(length, width);
printf("Area = %d\n", area1);
printf("\n--- Call by Reference (modify through pointer) ---\n");
int area2;
calculateAreaByRef(length, width, &area2);
printf("Area = %d\n", area2);
// Summary
printf("\n\n========== SUMMARY ==========\n");
printf("CALL BY VALUE:\n");
printf(" - Passes copy of variable\n");
printf(" - Changes inside function don't affect original\n");
printf(" - Safer but uses more memory\n\n");
printf("CALL BY REFERENCE:\n");
printf(" - Passes address of variable (using pointers)\n");
printf(" - Changes inside function affect original\n");
printf(" - More efficient for large data\n");
printf(" - Use & to pass address, * to access value\n");
return 0;
}
Comments
Post a Comment