|
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()
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.
This function, called Left, needs one value (a string) to do its job. It returns the leftmost letter of the string.
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.
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 FunctionFunctions are declared in the editor just like subroutines. Use the keyword Function:
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:
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:
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
ElseIf (dblNumGrade > 80) then
ElseIf (dblNumGrade > 70) then
ElseIf (dblNumGrade > 60) then
Else
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 GameWrite 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
Step 2: Set the properties For the scroll bar
lblLowScore.Caption = 1 lblHighScore.Caption = 100 lblValue.Caption = 50 Step 3: Write the Code Declare variable for Guesses (Static)
ElseIf (the guess > the secret number) Then
Else
End If
Homework Chapter 7 excercises 10, 11, 12. Add additional features to Word Processor.
|