FUNCTIONS

A user function is like a small sub-program within the whole program. Each function is used to perform a specific task. To improve the flexibility and consequently the benefit each function provides, it is written in a generalised way so that it can perform its task on a range of different values. These values are specified to the function as parameters. The programmer writes a function in such a way that it operates on the values passed to it when the function is invoked elsewhere in the program. The program must give each of the parameters his function will use, a name and a type. Parameters thus behave like local variables that belong to the invoked function but which are setup by the invoking code on behalf of the invoked function prior to invoking the function.

The parameters specified in the function definition are called the formal parameters. The parameters passed to the function when it is invoked are called the actual parameters.

The formal parameter list contains information regarding the position of the parameter within the list, the name the parameter will be refered to within the function and the type of the parameter.

NOTE: a formal parameter name can be the same as a previously defined global and the parameter definition will take precidence over the global definition of the variable throughout the scope of the function

A formal parameter list can be empty (no parameters) or can have any number of parameters. Each parameter in the list must be preceaded by its type which must be one of char, int and long.

NOTE: pointer parameters can also be passed to functions

Examples of valid formal parameters lists for the function fred would be

	proc fred()

	proc fred(int A, int B)

	proc fred(char B, int C, char A)

The following function called delay counts from 1 to N. Although the microcontroller counts very quickly it takes a very short measurable amount of time to adds 1 to its running total. The function therefor provides a short delay when it is invoked which is proportional to the value passed to it - its parameter (shown in red).

	proc delay(int N)

		int	count

		for count=1 while count<=N step count=count+1 do
		done

	endproc

The following function called flash pulses an output on for a short time. It uses the delay function to measure the on and off time. The flash function pulses the output N2 times. The number of pulses produced by this function is dependent on the value passed to it - its parameter (shown in blue).

	proc flash(int N2)

		int	count

		for count=1 while count<=N2 step count=count+1 do

			// turn output ON
			PORTA |= 1

			delay(100)

			// turn output OFF
			PORTA &= ~1

			delay(100)

		done

	endproc

The following function called main is the main section of code that is executed when the microcontroller is reset or switched on. Here it causes the flash function to be invoked which causes an out to be pulsed, once then twice then three times etc (until it reaches ten at which point it start from one again). Between each set of pulses there is a short pause which is produced using the same delay function that is used by the flash function however the delay period is much longer.
	proc main()

		int	j

		// set port pin to output
		TRISA = 0xFE

		// turn output OFF
		PORTA = 0

		while 1 do

			for j=1 while j<=10 step j=j+1 do

				flash(j)

				delay(500)

			done

		done

	endproc