Functions, Variables, and Parameters
Back
Defining a function is helpful in certain situations, but in others it is unnecessary. Take for instance the built- in functions one often finds coded into the programming language being used. These functions are stored in the language library, and can be used throughout the program without the need for explicit coded instructions to be carried out (take for instance the Val function, which checks to see if a variable is a string or numeric number). You can simply write in the syntax for the built in function, and it will be executed. Conversely, when needed the programmer can actually create a function. When doing so, the programmer must define the function- that is, name it and assign it arguments which allow it to execute to the desired effect. A function should be used generally when the submodule the function is placed in computes and returns a single value to the calling module (p 368 Extended Prelude to Programming).
When you are considering variables and parameters, it is important to remember what the scope of each variable you are using is limited to. Global variables are generally declared in the main module of a program or outside of all modules in a program, and have a scope of the entire program. Local variables, on the other hand, are declared in their specific subprogram and have a scope only of that subprogram. Thus, you can name a local variable and a global variable the same thing, and as long as the subprogram declares the local variable, you will have two separate variables using two separate memory location. Despite this, you can still use global variables to transmit data between modules, by simply not declaring the variable locally. Thus, any changes would be made to the global variable memory space, by any module that uses the global variable. However, as you might guess, this is generally not a good idea, because it diminishes the capabilities of the subprograms (modules) to act on their own if they are relying on a global variable, declared out of their specific module. Anything that effects that variable will affect all of the modules that use it. For simply problems this is ok, but otherwise it is a much better idea to use arguments and parameters to pass data between modules. This ensures that the subprograms are independent and change only what needs to be changed.
When actually using arguments and parameters, there are some things to keep
in mind. Remember that arguments are the data being sent by the calling module,
and that parameters are the variables where that data is placed. Now, in order
for this data to move between modules, you need to specify a parameter as one
of two types, either a Value parameter or a Reference parameter. More than one
type of parameter can be specified in the same line, as well. The value parameter
is used to import data into the subprogram or module- when the value of a value
parameter is changed, it does not effect the value of the argument (the corresponding
reference, from which the value parameter was garnered). The reference parameter
is used to both import data, and export data to other modules, and thus any
changed affected to its value will be exported to the calling argument. Although
value parameters may seem extraneous, they in fact are useful when you need
to import data that should not be changed, and can prevent inadvertent modifications
of variables you need to be static once assigned.