/* * ===================================================================== * Demonstrate use of "call by value" mechanism in function calls. * * Copyright (C) 1994-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written By : D. Mazzoni and M. Austin January 1994 * ===================================================================== */ #include int main( void ) { int iVal = 4, iReturnVal; int iVal1 = 1, iVal2 = 2; /* function prototypes */ int tryChange(int iVal); void changeTwoVars(int * ipVal1, int * ipVal2); /* [a] : Pass value of iVal */ printf("In main() : Passing value of iVal to \"tryChange\"\n" ); iReturnVal = tryChange(iVal); /* try to change iVal */ printf("In main() : Now iVal = %i iReturnVal = %i", iVal,iReturnVal); /* "iVal = 4 iReturnVal = 37" is printed */ /* [b] : Now pass references to variables */ printf("\nIn main() : Passing references to the function \"changeTwoVars\"\n" ); changeTwoVars( &iVal1, &iVal2 ); printf("In main() : Now iVal1 = %i iVal2 = %i\n", iVal1, iVal2 ); } tryChange(int iPassed) { enum{ AddVal = 103 }; /* [a] : Change a parameter that was passed by value */ printf("In tryChange() : iPassed = %i\n", iPassed); printf("In tryChange() : Adding %i to iPassed...\n", AddVal ); iPassed += AddVal; printf("In tryChange() : Now iPassed = %i\n", iPassed); return iPassed; } void changeTwoVars( int * ipFirst, int * ipSecond ) { enum{ Mult1 = 2, Mult2 = 10 }; /* [a] : Change two variables where a reference value has been passed */ printf("In changeTwoVars() : *ipFirst = %i *ipSecond = %i\n", *ipFirst, *ipSecond); printf("In changeTwoVars() : Multiplying arguments by %i and %d, respectively...\n", Mult1, Mult2 ); *ipFirst *= Mult1; *ipSecond *= Mult2; printf("In changeTwoVars() : Now *ipFirst = %i *ipSecond = %i\n", *ipFirst, *ipSecond); }