To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

# Part 3.5Structured programming

Code reuse is one of the most fundamental ways in which programming changed between the earliest computer programs and today's programming.

The term structured programming refers to a range of different techniques that are applied to the development of a computer program or code listing to ensure several key principles are met. Structured programming means making efficient use of several previously seen programming constructs such as decision making (if statements), loops and using subprograms/subroutines (functions or procedures).

## What is structured programming?

There are several aims of structured programming. Structured programming seeks to make code:

• More Reusable - code can be reused over and over
• Smaller - having only one instance of the code makes it smaller
• More readable - having only one instance of the code makes it much easier to read
• Flexible - particularly in relation to functions/procedures and loops, the way that the program flows can change based on some parameters.
• Higher performance - by reusing code, the program actually can run more smoothly
• Easier to test - in relation to reliability of code, if the code is tested over and over again and it works, why write the code over and over again? If it doesn't work in one place you would need to change it over the program, but implementing it in a function or procedures means you only need to test that instance of the code.

## Without code reuse

Code can be reused throughout a program, avoiding the need to rewrite it. Alternatively, it can be copied and pasted throughout the program.

Take a look at the following code sample which is used to calculate the power consumption of a typical CMOS transistor (in theory) at a specified switching rate given in gigahertz ($f) running at a voltage of 12.5V ($v) with a capacitance of 0.02µF ($c) based on the formula: $$P = CV^2f$$ YASS $c = 2000E-8
$v = 12.5$f = 2.3E9
$p = (($c * $v) ^ 2) *$f)
print($p)$f = 2.9E9
$p = (($c * $v) ^ 2) *$f)
print($p)$f = 1.8E9
$p = (($c * $v) ^ 2) *$f)
print($p)  The same formula is being used for each of these calculations but with different values each time (or at least the $f value is changing).

This is inefficient!

## Defining a subprogram

Subprograms need to be well-defined in programming languages and must serve a purpose. Subprograms generally are defined using a keyword such as function (VB.NET, PHP, YASS), def (Python), void (Java, C, C++, C#) or a data type from the language followed by an identifier (the name of the function), after which a set of brackets normally follows containing parameters. This lines tells the compiler what to do with the subprogram and is known as the signature.

YASS
	function calculateSqr($a,$b)


In this example, the function is the keyword that tells the compiler that this is a subprogram, calculateSqr is the identifier that allows the code to be reused, and there are three parameters within it ($a and $b).

## Using functions and procedures

A subprogram is a chunk of code that can be run over and over again. Subprograms can be either a function or a procedure. A function returns or gives back a value whereas a procedure simply runs some code and does not need to return a value (but it can manipulate or change the value of other variables within the application).

It is written just once but used many times in the code.

In YASS, both functions and procedures are created with the function symbol:

YASS
function calculatePower($c,$v, $f) return (($c * $v) ^ 2) *$f

end function

$voltage = 12.5$p = calculatePower(2000E-8, $voltage, 2.3E9) print($p)

$p = calculatePower(2000E-8,$voltage, 2.9E9)
print($p)$p = calculatePower(2000E-8, $voltage, 1.8E9) print($p)



Instead of writing the formula or calculation three times, the formula is written once. The section in lines 1 to 5 provides a reusable chunk of code known as a function. This function gives back the answer to the equation given some values.

When a subprogram is run, it is called.

## Parameters

A parameter is a special type of variable that relates to subprograms directly. There are two kinds of parameters:

• Formal parameters (often known simply as parameters)
• Actual parameters (also known as arguments)

Looking at the previous sample as defined on line 1:

YASS
function calculatePower($c,$v, $f)  In the signature of the function calculatePower there are three parameters required; $c, $v and $f. Within a computing context, particularly in relation to the formal lambda calculus, these parameters are said to be bound to the calculatePower function. This means that these parameters are basically variables within the scope of that subprogram and no further. If there is a variable with the same name out with the subprogram, then that variable remains untouched by any changes made to the variable within the subprogram.

Since these parameters are defined within the signature of the function, they are placeholders for actual values that will be 'passed' to the function when the function is called. They are therefore known as formal parameters.

Later on in the previous program, the function calculatePower is called three times:

YASS
$p = calculatePower(2000E-8,$voltage, 2.3E9)
$p = calculatePower(2000E-8,$voltage, 2.9E9)
$p = calculatePower(2000E-8,$voltage, 1.8E9)


Each time the function is called, the frequency value of the CMOS transistor has changed, from 2.3E9 to 2.9E9 to 1.8E9 (the E just represents an exponential value or 2.3x109, 2.9x109 and 1.8x109). In the all of the function calls there are three are three parameters, with only one changing. For the first call the parameters are 2000E-8, $voltage and 2.3E9. These are actual parameters since they are being passed to the function. These variables will become the values of the formal parameters defined within the function signature: • $c becomes 2000E-8
• $v becomes $voltage (which itself is substituted to 12.5)
• \$f becomes 2.3E9
Code preview
Feedback 👍
Comments are sent via email to me.