Call by Value vs Call by Reference

/*
Call by Value vs Call by Reference
 */

#include <stdio.h>

void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}

void rswap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

int main(int argc, char const *argv[])
{
int a, b;

printf("a = ");
scanf("%d", &a);

printf("b = ");
scanf("%d", &b);

printf("a = %d, b = %d\n", a, b);
rswap(&a, &b); // Call by reference
printf("a = %d, b = %d\n", a, b);

return 0;
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments