/* CPE 101 Fall 2008 */ /* Alex Dekhtyar */ /* Functions with out parameters */ #include #include /* We compare effects of functions with and without output parameters */ int add(int x, int y, int z); int plus(int x, int y, int *z); int main() { int x,y,z; x=3; y=5; z=0; add(x,y,z); printf("Outside add(.): %d+%d=%d\n", x,y,z); printf("\n"); plus(x,y,&z); /* must pass z as an output parameter using "&" */ printf("Outside plus(.): %d+%d=%d\n",x,y,z); /* note, in main(), we refer to z w/o "*" */ return 0; } /* add() does not have output parameters. We observe its effects within the function */ /* but not outside it */ int add(int x, int y, int z) { z = x+y; printf("Inside add(%d,%d). z = %d\n",x,y,z); return 0; } /* plus() has an output parameter. Its effects are observed both within and */ /* outside the function */ int plus(int x, int y, int *z) { *z = x+y; printf("Inside plus(%d,%d). z = %d\n",x,y,*z); /* note, must refer to contents of z via "*z" */ return 0; }