You may have noticed in the last example program that we had to create two sets of If/then/else/endif statements to handle the conversion of a grade into a letter grade. This duplication of code is a bad practice. If there are several of these blocks of code, and we find a bug in one of them, we have to update all of them. For example, what if, in our example program, we decided that the grade cutoffs were 90, 80, 70, and 60? We would have to make the modification in each of the two blocks of If statements. This could lead to extra work, or even errors in our program if we forget to make the same changes in both places.
Fortunately, B2C has a concept called a Subroutine. In the old days, a program was called a routine. A Subroutine therefore is a routine within a routine. You can declare a subroutine with the word Sub:
Sub [routine-name] [(var as type, var as type, )] 'statements End Sub
The Parameters portion of the Subroutine is a list of variables (declared much in the same way as the Dim statement). These variables are the inputs to the subroutine. Once declared, a subroutine can be called (or invoked) with the Call statement
[Call] routine-name [(parameters)]
The keyword Call is optional.
Here is an example using our grade-averaging program:
Sub letterGrade(grade as int) if grade >=94 then print grade, "=A" elseif grade>=86 then print grade,"=B" elseif grade>=80 then print grade, "=C" elseif grade>=74 then print grade, "=D" else print grade, "=F" end if End Sub
You may exit a Subroutine early by executing the command Exit Sub.
Another way to solve this problem is with a Function. A function is like a subroutine, but it returns a value. In our example a function could return the letter grade, given the numeric grade. To return a value, you set the name of the function to the value, just like an assignment statement. As with the Subroutine, you may exit a function early with the Exit Function command.
Function letterGrade(grade as int) as char if grade >=94 then letterGrade = 65 ascii characters but we didnt teach it elseif grade>=88 then letterGrade = 66 elseif grade>=80 then letterGrade = 67 elseif grade>=74 then letterGrade = 68 else letterGrade = 70 end if End Function
Example program:
DO THIS |
Copy the
file "c:\
\B2Cv5\tutorial\ch12.b2c" to "C:\
\B2Cv5\ch12.b2c".
Then execute the command "build ch12.bld". Download the ch12.app file
to the cybiko
Subroutines allow us to simplify our program by taking similar sections of code and condensing them into one subroutine. |
' chapter 11 example program ' sum and average of n grades ' grades are from 0-100 Sub letterGrade(grade as double) if grade >=94 then print grade, "=A" elseif grade>=86 then print grade,"=B" elseif grade>=80 then print grade, "=C" elseif grade>=74 then print grade, "=D" else print grade, "=F" end if End Sub dim sum 'this variable will store the sum of the n grades dim avg 'this variable will store the average of the n grades dim n as int ' the number of grades to average print "How many grades?" input n dim grade[n] ' an array of n grades ' ---INPUT --- for i=0 to n-1 'get the inputs print "Enter grade ", i ' notice that we indent in loops input grade[i] 'get the grades from the student next '--- PROCESS --- ' compute the sum and the average for i=0 to n-1 print "Grade ", i, ": ", grade[i] call letterGrade(grade[i]) sum = sum + grade[i] 'notice we accumulate the sum next avg = sum/n '--- OUTPUT --- print "The average of your" print n, " grades is ", avg call letterGrade(avg) print "Press <Enter> to continue" dim tmp ' a temporary variable input tmp 'wait for the user to press enter |