Please enable JavaScript.
Coggle requires JavaScript to display documents.
Higher Software Development (Benefits of Procedures and Functions (Using…
Higher Software Development
Benefits of Procedures and Functions
Using procedures and functions lets you structure complex programs. There are many benefits of using them such as:
Procedure code and be saved and re-used in future projects by creating a module library.
Repetition of lines of code is avoided.
Testing of the procedure code can take place in isolation from the main program.
Debugging of the main program is simplified as each procedure can be individually tested.
Functions
A function is activated by a call from the main program. A function returns a single value. As well as using pre-defined functions you can also write your own functions.
A function consists of:
A heading
A declaration part
An action part
Procedures
Procedures are activated by a call from the main program. When a procedure is called it produces an effect. A procedure consists of:
A heading
A declaration part
An action part
Formal and Actual Parameters
When you use parameters you refer to them as formal or actual.
A formal parameter is used as a placeholder within a subprogram for the values received or sent to the main program’s actual parameters.
An actual parameter is the actual value that is passed into the procedure.
Global Variables
A global variable can be accessed and changed from any part of the program, even from another script/event so long as it is declared at the very start.
Global variables should be used carefully because their values may accidentally change if the programmer forgets that they have already used them in another subroutine.
Local Variables
A local variable exists within one subroutine, function, or procedure.
Local variables are created when the subroutine is called and are then destroyed when the subroutine terminates.
They cannot be accessed or assigned a value except within that subroutine.
Advantages of Local Variables
The advantage of using local variables is that:
It prevents them from being used elsewhere in the program
It prevents them from having their contents accidentally changed
The same variable name can be used more than once in different parts of the program
They aid modularity of a program
Scope of Variables
The scope means where the variable is visible in the program.
For a local variable the scope is limited to a single procedure or function.
For a global variable the variable is visible throughout the whole program. This means the scope is the whole program.
Passing By Value
Passing a parameter by value is used when a parameter is needed in a subroutine but its value is not going to change in the subroutine.
The subroutine will be passed a copy of the original parameter, so that the original parameter remains unchanged.
Passing by reference
Passing a parameter by reference is used when a parameter is needed in a subroutine and its value is going to be changed in the subroutine when it is passed in.
The subroutine will be passed the original parameter and any changes made in the subroutine will result in a change to the original value held within the parameter.
Passing Arrays
Around forty years ago, when high level languages were becoming popularity, the designers of these languages decided to only support passing arrays by reference. This is because passing an array by value would place too much pressure on the RAM. Arrays still tend to be passed by reference because of this.