You are here: start » ppl » statements » function

FUNCTION

FUNCTION

	[DECLARE] FUNCTION func(TYPE var1, ...) TYPE

The keyword PROCEDURE is used in conjonction with the keyword FUNCTION in the declaration of a user-defined function…
The compiler directive “;$USEFUNCS” may be used in order to allow your main code (code between BEGIN & END) to be located anywhere within your file…
The big difference between functions and procedures is that functions return a value. To assign the return value inside a function, simple use the name of the function just like a variable. You do not need to declare this variable, it is done for you. When the function is finished executing the value in the return variable will be made available as the return value.
Note that function calls can take place anywhere inside of an expression as well as stand-alone statements. This can be useful in situations when the functions return value is not needed, but the functions side effects are.
Example:

	;$USEFUNCS
	DECLARE FUNCTION Xto_theY(INTEGER x, INTEGER y) INTEGER
	DECLARE FUNCTION square(INTEGER x) INTEGER
	FUNCTION Xto_theY(INTEGER x, INTEGER y) INTEGER
	INTEGER i
	 Xto_theY = x
	 for i = 2 to y
	   Xto_theY = Xto_theY * x
	 next i
	ENDFUNC
	FUNCTION square(INTEGER x) INTEGER
	square = x * x
	ENDFUNC
	BEGIN
	  PRINTLN "4 to the 3rd power = ",Xto_theY(4,3)
	  PRINTLN "4 squared = ",square(4)
	END

See also: Procedure

ppl/statements/function.txt · Last modified: 2019/04/16 14:00
CC Attribution 4.0 International Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution 4.0 International