To simplify the programming task, the structured programming movement stressed breaking the large problem (program) into smaller parts (sub-programs). There are two types of subprograms, procedures (subroutines) and functions. Recall how procedures work:

Sub Form_Load()

Task1
Task2
Task3

End Sub

Sub Task1()

    MsgBox "This is task 1"

End Sub

etc.

Each subroutine is called by name, performs its task, and returns to the calling procedure. Think of subroutines as "helpers" who perform a specific task when called then return to where they started.

Functions are quite similar in that they are called to perform a specific function. The difference is, when they return, they bring back a value. Like procedures, functions may or may not need certain values (parameters) to do their job. Functions only have one job- to find a specific value. Recall that we have used functions.

strLetter = Left("Ray", 1)

This function, called Left, needs one value (a string) to do its job. It returns the leftmost letter of the string.

intNum = Val(strNum)

This function, called Val, needs on value (a string) to do its job. It returns the integer value of the string.

These are functions because they return a single value!

There is also a difference in how functions are called. Since they always return a value, you as the programmer, must do something with that value. Notice that procedure calls, like the ones above, appear on a line all by themselves. Also note that functions are always on the right hand side of an equal sign. Recall that we have a command that is also a function, MsgBox. When it appears on a line by itself, it is a procedure. When it is on the right hand side of an assignment statement, it is a function.

MsgBox "Hello, Word"

strResponse = MsgBox("Want to continue", vbYesNo + vbQuestion, "Continue")

The first example is a command, the second is a function.

Notice that functions look different than commands. Functions use parentheses to pass parameters, procedures do not (unless you use Call- which I don't want you to do).

Declaring Function

Functions are declared in the editor just like subroutines. Use the keyword Function:

Function <Function Name> ([<parameter list>]) As <datatype>

For example, lets design a function that assigns letter grades. What information does the function need to calculate this? (Right- the numerical grade). Thus we want a function that performs like this:

Numeric Grade -->

   FindLetterGrade 

 --> Letter Grade

Now, lets think about data type for a moment. The input parameter is a numeric grade. What data type should it be? Well, that depends on the specifications of the problem. Are you a teacher that gives "half points?" If so, use the Double data type, otherwise you can use Integer. (I'll use Double.) The value returned (LetterGrade, is a string). So, here we go:

Function FindLetterGrade(ByVal dblNumGrade as Double) As String

Looks confusing, doesn't it? Remember- both the data type of the input (parameter) and data type of the returned value must be defined. To continue,

Function FindLetterGrade(ByVal dblNumGrade as Double) As String

If (dblNumGrade > 90) then

FindLetterGrade = "A"

ElseIf (dblNumGrade > 80) then

FindLetterGrade = "B"

ElseIf (dblNumGrade > 70) then

FindLetterGrade = "C"

ElseIf (dblNumGrade > 60) then

FindLetterGrade = "D"

Else

FindLetterGrade = "F"

End If

Notice that the value calculated by the function gets assigned a certain value. We say that this value is returned to the calling function.

Case Study: The Hi-Low Guessing Game

Write a program that plays a guessing game with the user. The computer generates a random number between 1 and 100 and the user tries to guess the number. If the user guess is too high, they should get a messages saying "Too high." If the guess is too low, it should say "Too low." When the user gets it right, the computer should say "You guess it right in X guesses." ("X" of course is the number of guesses.

The program will use a horizontal scroll bar to get the user input. After a wrong guess, the range of the scroll bar should change to reflect the guess.

Step 1: Design the Interface

The objects include

bullet

a form

bullet

6 labels

bullet

a horizontal scroll bar

bullet

a command button

Step 2: Set the properties

For the scroll bar

bullet

Min = 1

bullet

Max = 100

bullet

Value = 50

lblLowScore.Caption = 1 

lblHighScore.Caption = 100

lblValue.Caption = 50

Step 3: Write the Code

Declare variable for Guesses (Static)
Increment guesses 
If (the guess  > the secret number) Then

Give "Too high" message
set max property of scroll bar to Guess - 1
set "High" label to value of scroll bars

ElseIf (the guess  > the secret number) Then

Give "Too low" message
set min property of scroll bar to Guess + 1
set "Low" label's caption to value of scroll bar

Else

Give "Right. It took you " & Guesses & " guesses." message

End If

Homework

Chapter 7 excercises 10, 11, 12.

Add additional features to Word Processor.