CS 010 - Introduction to Computer Science I
Lab 8.
Functions and Parameter Passing

Points (10 overall)

  1. 2 pts: Attendance(at start and end of lab)
  2. 7 pts: Programs
  3. 1 pt:  Commenting and Style

You will be working in pairs during labs. Pairs will be selected and announced by the TA. You will discuss the programs with your partner, but you will still be typing in your own code to show to the TA. You can help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc


Lab Objectives

To gain experience with

  1. writing functions
  2. using functions
  3. pass-by-value and pass-by-reference parameters

Commenting and Style

  1. Be sure to use good variable names.
  2. Use comments throughout your code to explain your code.
  3. Use good spacing and keep it consistent throughout your program.

 


Program 1: Reference Parameters

Write a swap function to swap the values of two integers. 

 

Write an order function that takes three integers and guarantees that they are stored in the parameters in ascending order upon completion of the function. This function must call the swap function when appropriate.

 

Write a short program to verify correctness.


Program 2: Mixed (Value and Reference) Parameters and a Return Type of Void

Write a program that allows the user to enter a number (0-999) and output each digit separately.  The user should be able to enter numbers and see the digits as often as they want.  A negative number will be used to quit the program.  Values over 999 should simply be stated to be invalid input.

 

For this program, you should have at least one function called find_digits.  Since three values need to be calculated and a function can only return one value, you should use pass-by-reference to “return” the digits.  Thus the function should have a return type of void and take 4 parameters, the number, and a reference for each digit.


Program 3: Mixed (Value and Reference) Parameters and a Non-Void Return Type

Write a function called transaction that simulates a bank transaction.  It should take as parameters the current amount in the account, an integer code to represent a deposit (1) or withdrawal (2), and the amount to be withdrawn or deposited.  This function should perform the transaction, adjusting the current amount.  Do not let the account be overdrawn (have a negative balance).  If a transaction cannot be performed, the function should return a 0.  If the transaction was able to be completed without errors, a 1 should be returned.  Be sure to use good parameter passing for each parameter.

 

Write a short program to verify this function, allow the user to perform as many deposits or withdrawals as they want, give a menu to allow them to continue choosing between (making a deposit, making a withdrawal, or quitting the program).