Passing arguments by value and by reference. - C Function

C examples for Function:Function Parameter

Description

Passing arguments by value and by reference.

Demo Code

#include <stdio.h>

void by_value(int a, int b, int c);
void by_ref(int *a, int *b, int *c);

int main( void )
{
    int x = 2, y = 4, z = 6;

    printf("\nBefore calling by_value(), x = %d, y = %d, z = %d.", x, y, z);

    by_value(x, y, z);/*from  w w w . j a v  a  2 s .c  o  m*/

    printf("\nAfter calling by_value(), x = %d, y = %d, z = %d.", x, y, z);

    by_ref(&x, &y, &z);
    printf("\nAfter calling by_ref(), x = %d, y = %d, z = %d.\n", x, y, z);
    return 0;
}

void by_value(int a, int b, int c){
    a = 0;
    b = 0;
    c = 0;
}

void by_ref(int *a, int *b, int *c){
    *a = 0;
    *b = 0;
    *c = 0;
}

Related Tutorials