Tutorial - Functions

In this tutorial we are going to look at functions and how to build your own. There are a variety of ways of defining and using parameters. In this tutorial I will cover just the most basic mechanism.

Using Functions

Functions are just like functions in mathematics. They are named operations that takes a number or arguments ( parameters ) and returns a value.

Program( )
	Number y;
    Begin
	y := Sin(30);
	Output y;
    End;

For normal people angles are measured in degrees - so GraRLS uses degrees rather than radians, mathematicians can write their own wrapper functions to use radians.

With the mechanism covered here all the parameters are specified in the order they appear in the documentation and will be the order in which they are listed in the code defining the function.

Defining Functions

Defining your own functions is similar to defining a Shape except we need a way or returning the result.

Function return_type function_name ( parameters )
	local variables
    Begin
	
	some code

    End;

the code section must contain at least one statement of the form :-

	Return expression;

The expression must yield a result of the type specified for the function.

A function never draws anything and so does not have a graphics context.

Program( )
	Number y;
    Begin
	y := Squared(5);
	Output y;
    End;

Function Number Squared(Number x)
    Begin
	Return x * x;
    End;