Functions & Procedures

Functions and procedures are the basic building blocks of programs. They are small sections of code that are used to perform a particular task, and they are used for two main reasons.

The first reason is that they can be used to avoid repetition of commands within the program. If you have operations that are performed in various different parts of the program, then it makes sense to remove the repeated code and create a separate function or procedure that can be called from those places instead. Not only will this reduce the size of your program, but it will make the code easier to maintain, and it will remove the possibility that some of the code segments are updated, but not others.

This modular approach also facilitates data abstraction (see the variables page) by centralising data-storage functions.

The second reason for careful use of functions and procedures is to help define a logical structure for your program by breaking it down into a number of smaller modules with obvious purposes. Depending on the programming language you use, you can also compile a library of functions and procedures and import them for use in other programs.

Are Functions and Procedures the Same Thing?

Functions and procedures are very similar - in fact, in some programming languages there are only functions, and procedures are seen as a special case of a function, just as a square is a special type of rectangle. Generally speaking, functions return a value, whereas procedures don't (so a procedure is just a function that doesn't return a value). To confuse matters, though, there are certain subroutines, such as msgbox() in VisualBASIC that appear to be both/either (if you specify only the message, msgbox() behaves as a procedure, but if you use the extra options, such as message type, then msgbox() returns a value).

By returns a value, we mean that the function creates some sort of results, which is passed back to the calling function. A subroutine called squared(), for example, that calculates the square of a number, would be a function, because the result of the calculation would need to be passed back.

Variables, Functions and Variables

Variables declared within functions or procedures are said to be local - that is, they can only be used within that function, or other functions called by that function. This is called the scope of the variable (see the variables page).

Local variables are destroyed when the function or procedure finishes executing, and their values are lost. Next time you call the function/procedure, the variable is recreated and reset (usually to zero). If you want your local variables to retain their values when the function or procedure is not being executed, you need to declare it to be static.

Arguments

Some functions will require arguments - values upon which the operation performed by the function will be based. If you are using a typed language, then you will need to give the argument a type in your function/procedure declaration. Here are sample function declarations in JavaScript and VisualBASIC:

JavaScript:

function factorial(n)
{

	if (n == 0) 
	{ 

		return 1;

	}
	else 
	{

		return n * factorial(n - 1);

	}

}

VisualBASIC:

Function factorial(n as integer)

	if n = 0 then 

		factorial = 1

	else 

		factorial = n * factorial(n - 1)

	End if

End Function

Notice that we don't need to declare n because it's implicitly declared in the function declaration.

Changing the value of the argument (in this case n) in the function may or may not change the value of the value of variable used to pass the argument from the calling procedure, depending on the circumstances. If the argument is passed by value, then a copy of the variable is created and passed to the function - changes to its value will therefore not affect the value in the calling procedure. If, on the other hand, the value is passed by reference, then the function or procedure is able to change the value of the variable passed. Most programming languages (including C/C++ and VisualBASIC) allow you to choose to pass values either by value or by reference.

Scratch supports procedures - they are called blocks, and are created in the More Blocks section.  This could be a good introduction to the idea of procedures for KS3 Computing students.  I have used blocks recursively to create my Trees and Natural Trees examples, and in a more simple way for Simple Spirograph. See how Blocks are using in Scratch on the Advanced ICT YouTube channel.