Lecture 16 - Subroutines & Functions


Home > Lectures > Lecture16

Objectives

Contents


1. Subroutine Example 3 - Compare Numbers

Problem Description

Write a program that allows a user to input three numbers and then displays the smallest number. This technique is similar to that used in latter programming subjects to find the smallest in a list of values.

Example Run

Run
Number 1 ? 5
Number 2 ? 3
Number 3 ? 8
Smallest number = 3

Defining Diagram

Inputs Processing Output
number1, number2, number3 Input number1, number2, number3
Determine the smallest number
Display smallest
smallest

Outline the Solution

 

 

 

Pseudo Code

 

 

 

 

 

 

Data Dictionary

Name Data Type Description
number1 Double The first number to be compared
number2 Double The second number to be compared
number3 Double The third number to be compared
smallest Double The smallest of the three numbers

Desk Check

Input data: number1 = 5, number2 = 3, number3 = 8; Correct result smallest = 3

Line Number number1 number2 number3 smallest Conditions Input/Output
             
             
             
             
             
             
             

Code

' Name: CompareNumbers
' Purpose: Compare 3 numbers and display the smallest
' Author: Tim Whitfort

Option Compare Database
Option Explicit

' Declare global variables.
' Global variables are known throughout the program
Dim number1 As Double  ' First number to be compared
Dim number2 As Double  ' Second number to be compared
Dim number3 As Double  ' Third number to be compared
Dim smallest As Double ' The smallest of he number input


Sub Main()

    ' Call the inputNumbers subroutine
    inputNumbers

    ' call the getSmallestNumber subroutine
    getSmallestNumber

    ' Call the displaySmallest subroutine
    displaySmallest

End Sub


' Input the 3 numbers
Sub inputNumbers()

    number1 = InputBox("Number 1 ?")
    number2 = InputBox("Number 2 ?")
    number3 = InputBox("Number 3 ?")

End Sub


' Determine the smallest number
Sub getSmallestNumber()

 
 
 

End Sub


' Display the smallest of the 3 numbers
Sub displaySmallest()
    Debug.Print "Smallest is "; smallest
End Sub

2. Subroutine Example 4 - Swap Two Values

Problem Description

Write a program that uses a subroutine to swap two values. This technique is used in latter programming subjects to sort a list into order.

Example Run

Run
Number 1 ? 5
Number 2 ? 3
After swap
Number 1 = 3
Number 2 = 5

Defining Diagram

Inputs Processing Output
number1, number2 Input number1, number2,
Swap number1 and number2
Display smallest
number1, number2

Outline the Solution

We can't just assign number2 to number1 and vice-versa as this would loose the original value of number1. A temporary variable is needed to hold the value during the swap.

Pseudo Code

swapTwoNumbers()
    inputNumbers()
    swap()
    displayNumbers()
STOP
inputNumbers()
    Input number1, number2
EXIT
swap()
    temp = number1
    number1 = number2
    number2 = temp
EXIT
displayNumbers()
    Display number1, number2
EXIT

Data Dictionary

Name Data Type Description
number1 Double The first number to be compared
number2 Double The second number to be compared
temp Double Temporary storage for number1

Desk Check

1  swapTwoNumbers()
2     inputNumbers()
3     swap()
4     displayNumbers()
5  STOP
6  inputNumbers()
7      Input number1, number2
8  EXIT
9  swap()
10     temp = number1
11     number1 = number2
12     number2 = temp
13 EXIT
14 displayNumbers()
15     Display number1, number2
16 EXIT

Input data: number1 = 5, number2 = 3; Correct results: number1 = 3, number2 = 5

Line Number number1 number2 temp Input/Output
         
         
         
         
         
         

Code

' Name: SwapTwoNumbers
' Purpose: Input 2 numbers, swap them then display the new values
' Author: Tim Whitfort

Option Compare Database
Option Explicit

' Declare global variables.
Dim number1 As Double 
Dim number2 As Double 

Sub Main()
    inputNumbers
    swap
    displayNumbers
End Sub

' Input the 2 numbers
Sub inputNumbers()
    number1 = InputBox("Number 1 ?")
    number2 = InputBox("Number 2 ?")
End Sub

' Swap the two numbers
Sub swap()
    ' Local variable. Temporary storage for number1
    Dim temp As Double 
    temp = number1
    number1 = number2
    number2 = temp
End Sub

' Display the numbers
Sub displayNumbers()
    Debug.Print "After swap"
    Debug.Print "Number 1 = "; number1
    Debug.Print "Number 2 = "; number2
End Sub

3. Functions

We have been using functions for some time!

Functions are a special type of subroutine that return a single value. We won't be writing functions in this subject, however we will look at a few functions that are provided by Basic.

InputBox function and the Format function are examples of functions that we have been using.

InputBox Function

A parameter is supplied (passed to) the function to provide data required for the function to work, in this case the input prompt to be displayed.

A return value is the result that we are interested in finding out. In this case it is the value input by the user.

Example

Dim cost As Double
cost = InputBox("Cost of an item ? ")

In the above example:

Format Function

Example

costPer100Grams = 12.3 
Debug.Print Format("Cost per 100 grams = "; Format(costPer100Grams, "0.00")

In the above example:

Mathematical Functions

Here are some examples of mathematical functions provided in Basic:

Round Function

Example

Dim cost As Double
cost = 12.347
cost = Round(cost, 2) ' cost is now 12.35

Sqr Function

Example

Dim y As Double
y = Sqr(x)

Example

' Name: MathsFunctionDemo
' Author: Tim Whitfort
' Purpose: Demonstrate using built-in functions

Option Compare Database
Option Explicit

Sub Main()

    ' Local Declarations
    Dim price As Double ' The price of an item before tax
    Dim tax As Double ' The GST on an item
    Dim x As Double ' A number
    Dim y As Double ' The square root of x

 
 
 

End Sub

What's Next?

That's the end of the lectures on Software Development. For the next few weeks we will be looking at the UNIX operating system.

Key Points

Further Reading


Previous Lecture | Lecture Index | Next Lecture | Tutorial
Last modified 23-Mar-2006 by Tim Whitfort.
Copyright © 2003-2006
Tim Whitfort