224 Passing Parameters This is how the preceding subprogram works: ✦ The first line defines the subprogram name — ConvertC2F — and its parameter list as accepting one Single variable called Temperature. To specify that this parameter will be passed by reference, this BASIC language example uses the ByRef keyword. ✦ The second line plugs the value of the Temperature variable into the conversion equation and stores the result back in the Temperature variable, erasing the preceding value that was stored there. ✦ The third line ends the subprogram. At this point, the modified value of the Temperature variable is sent back to the main program to use. Every programming language uses different ways to identify when a parameter will be passed by reference. The BASIC language uses the ByRef keyword whereas the C language uses the ampersand symbol (&) to identify parameters passed by reference. In the following C example, the a parameter is passed by value but the x parameter is passed by reference: subprogram_example (int a, float &x); If you had the following BASIC subprogram: SUB ConvertC2F (ByRef Temperature as Single) Temperature = ((9/5) * Temperature) + 32 END SUB You could call that subprogram like this: DIM Temp AS SINGLE Temp = 12 PRINT “This is the temperature in Celsius = “; Temp ConvertC2F (Temp) PRINT “This is the temperature in Fahrenheit = “; Temp END Running this program would produce the following: This is the temperature in Celsius = 12 This is the temperature in Fahrenheit = 53.6 Notice that right before calling the ConvertC2F subprogram, the value of the Temperature variable is 12, but the ConvertC2F subprogram changes that value because the subprogram was passed the Temperature value by reference. What happens if you run the same program except change the subprogram to accept parameters passed by value instead, such as
Passing Parameters 225 DIM Temp AS SINGLE Temp = 12 PRINT “This is the temperature in Celsius = “; Temp ConvertC2F (Temp) PRINT “This is the temperature in Fahrenheit = “; Temp END SUB ConvertC2F (Temperature as Single) Temperature = ((9/5) * Temperature) + 32 END SUB This program would print the following: This is the temperature in Celsius = 12 This is the temperature in Fahrenheit = 12 Book II Chapter 6 Although the ConvertC2F subprogram changed the value of the Temperature variable, it never passes the changed value back to the main program. So the main program blissfully uses the current value of the Temperature variable, which is always 12. Passing data by reference means that the subprogram can change any data used by another part of a program. This can increase the chance of problems because the more ways data can be changed, the harder it can be to track down errors. Breaking a Large Program into Subprograms Storing values in a subprogram name One problem with passing parameters by reference is that you may not always know when a subprogram will change its parameter values. To make it clear when a subprogram returns modified data, you can create a special type of subprogram called a function. A function is nothing more than a subprogram with the subprogram name representing a value. So a typical function might look like this: FUNCTION Name (parameter list) AS DataType Commands RETURN value END FUNCTION In BASIC, you identify a function with the FUNCTION keyword to define a subprogram as a function. After listing a parameter list, the first line also defines the data type that the function name can hold, such as an integer, a string, or a single (decimal) number.