Source Code. Projects. Nerd Stuff. Art Stuff.

Lesson 8: Advanced Functions

Advanced Functions

Parameter Passing Mechanisms: Call-By-Reference vs. Call-By-Value

  • Call-by-Value- the default parameter-passing mechanism for all datatypes except for arrays. In this mechanism, the argument and the parameter occupy separate memory spaces, and the value of the argument is copied to give the parameter its initial value. This way, changes to the parameter’s value are local to the function, and don’t affect the argument. Example:
        int fac(int x)  // retuns factorial of x (a.k.a. x! )
        {
          int ans = 1;
          while(x > 1)
          {
            ans *= x;
            x--;
          }
          return ans;
        }
    
        int main()
        {
          int x = 3;
          cout << fac(x);
          cout << " is " << x << " factorial.\n";
          return 0;
        }
        /* Output:
        6 is 3 factorial.
        */
  • Call-by-Reference- specified by an & following the datatype. In this mechanism, the argument and the parameter use the same memory space. So changes to the parameter are changes to the argument as well. Example:
        int fac(int& x)  // retuns factorial of x (a.k.a. x! ) and changes x
        {
          int ans = 1;
          while(x > 1)
          {
            ans *= x;
            x--;
          }
          return ans;
        }
    
        int main()
        {
          int x = 3;
          cout << fac(x);
          cout << " is " << x << " factorial.\n";
          return 0;
        }
        /* Output: (notice x has changed, making the following statement false)
        6 is 1 factorial.
        */
  • The argument for a call-by-reference parameter must be changeable. It can’t be a const, literal, or some other non-variable expression.
  • A version of fun_interest.cpp that uses a function with reference parameters to input 3 numbers from user.
  • In-class exercise: write a program that inputs age and year, and determines birth year, using reference parameters as needed. Here is the sample output:
      How old are you? 34
      What year is it now? 2007
      Did you have your birthday yet (y or n)? n
      Your were born in 1972
    
      $ a.out
    
      How old are you? 20
      What year is it now? 2011
      Did you have your birthday yet (y or n)? y
      Your were born in 1991

    And here is the main function you should use:

      int main()
      {
        bool birthdayPassed;
        int userAge, currentYear;
    
        birthdayPassed = getBirthInfo(userAge, currentYear);
        outputBirthYear(birthdayPassed, userAge, currentYear);
    
        return 0;
      }

    Your job is to write the two functions getBirthInfo and outputBirthYear.

  • Solution to above in-class exercise determining birth year
  • Example program: Blackjack program: complex program using several functions, some with reference parameters.
  • In-class exercise: write the rest of this program. Do not change the main function, except to enter code where commented:
      /* Sample Output:
    
      Enter a name: Craig
      Enter Craig's age: 39
      Do you want to enter another? y
      Enter a name: Serafina
      Enter Serafina's age: 8
      Do you want to enter another? n
      Craig is the oldest
    
      */
    
      int main()
      {
        int userAge, oldestAge = 0;
        char userName[20], oldestName[20];
    
        do{
          getNameAndAge(userName, userAge);
    
          // Enter code here to set oldestAge, oldestName
    
        }while(askIfUserWantsToEnterAnother());
    
        cout << oldestName << " is the oldest\n";
    
        return 0;
      }
  • Solution to above in-class exercise to find oldest person