#include using namespace::std; // In C++ there are 4 ways a function can return an answer to the caller // All of our functions will calculate the answer as 1 added to the first parameter // 1. return the answer on the stack // NOTE: the caller can save the returned answer by using the = operator int f1(int parm); // 2. place the answer in the 2nd parameter void f2(int parm, int & answer); // Why the & ? // & makes argument a call by reference and the function has access to callers memory // instead of pass by value, where function is passed the value. // 3. place the answer in the memory location pointed to by the 2nd parameter // NOTE: the 2nd parameter is for the address of the answer, // not the answer itself // NOTE: the caller acquires the memory for the answer void f3(int parm, int * pointerToAnswer); // 4. return the address of the answer on the stack // NOTE: the function must acquire the memory for the answer int * f4(int parm); void main() { // investigate f1 cout << "f1(15) should be 16:\n"; cout << f1(15) << endl; // printed value is 16 // NOTE: value parameters can have constants int ans; // call f1 and save the answer in ans and print ans // STUDENT WRITES THE CODE ans = f1(0); cout << ans << endl; // function chaining - a parameter will be a function call // sqrt(sqrt(16.0)) returns an answer of 2.0 // using f1 multiple times and the constant 22 have the value of 25 printed // STUDENT WRITES THE CODE cout << "f1(f1(f1(22))) should be 25:\n"; cout << f1(f1(f1(22))) << endl; // investigate f2 // declare an integer, call f2 using a first parameter of 35 and print the answer // STUDENT WRITES THE CODE int varf2; f2(35, varf2); cout << "Variable varf2 passed-by-reference to f2() should be equal to 36:\n"; cout << varf2 << endl; // investigate f3 // PART A - work with integers int theAns; // call f3 using a first parameter of 42 and have the answer placed in theAns // then print theAns // STUDENT WRITES THE CODE f3(42, & theAns); cout << "theAns should be 43:\n"; cout << theAns << endl; // PART B - work with pointers int * pointer; // acquire at run time the memory for an integer pointer = new int; // call f3 using a first parameter of 117 and have the answer placed in the // memory pointed to by pointer // the print the answer f3(117, pointer); cout << "The pointer should be 118:\n"; cout << *pointer << endl; // return the memory delete pointer; // investigate f4 // PART A // without saving the return value call f4 with the first parameter 234 // the value of 235 should be printed // STUDENT WRITES THE CODE cout << "The f4(234) should be 235:\n"; cout << *f4(234) << endl; // PART B // declare a pointer variable, call f4 with the first parameter 319 // and save the return value in the pointer variable // then print the answer -- the value 320 should be printed // STUDENT WRITES THE CODE int * pointerVar; // pointer variable declaration // pointerVar = new int; INSTRUCTOR NOTE: NO NEED FOR NEW, FUNCTION ACQUIRES THE MEMORY pointerVar = f4(319); cout << "The f4(319) should be 320:\n"; cout << *pointerVar << endl; // release the memory that the function acquired // STUDENT WRITES THE CODE delete pointer; } // write the functions // STUDENT WRITE THE CODE int f1(int parm) { // return 1; // DUMMY CODE TO GET TO COMPILE parm += 1; return parm; } void f2(int parm, int & answer) { answer = parm; answer+= 1; return; } void f3(int parm, int * pointerToAnswer) { *pointerToAnswer = parm; *pointerToAnswer += 1; return; } int * f4(int parm) { int * tempInt; // create pointer to type int tempInt = new int; // set pointer to address of nameless variable in freestore (a.k.a. heap) *tempInt = parm; *tempInt += 1; return tempInt; // return &parm; // DUMMY CODE - DO NOT USE!!!! only to get a compile } /* Output: f1(15) should be 16: 16 1 f1(f1(f1(22))) should be 25: 25 Variable varf2 passed-by-reference to f2() should be equal to 35: 35 theAns should be 42: 42 The pointer should be 117: 117 The f4(234) should be 235: 235 The f4(319) should be 320: 320 Press any key to continue . . . */