Function parameters

Functions, just like commands can have parameters associated with them. Here's an example of a function that takes two parameters.

do
	myFunction ( 10, 20 )
	
sync ( ) loop
function myFunction ( a as integer, b as integer ) print ( a * b ) endfunction

The function has been declared using the function keyword and given a name, then within the brackets parameters can be declared. The declarations are the same as when declaring a variable (except no default value is assigned to them - they will get their values from actual function calls). With this particular function we have a and b declared as integers. This means that when this function is called it must be passed two integer parameters. The first parameter's value will be assigned to variable a, while the second parameter's value will be assigned to variable b. These parameters can be used within the function and treated as regular variables, as shown with the call to the print command that multiplies the variables a and b, displaying the result on screen.

The call to myFunction is made within the do loop, passing in 10 and 20. The value 10 is the first parameter and therefore inside the function a will have a value of 10. The value 20 is the second parameter and therefore inside the function b will have a value of 20.

By default variables declared within AppGameKit are defined as integers, so the function declaration would still be valid if it was written like this.

function myFunction ( a, b )
	print ( a * b )
endfunction

The next program shows the same function, but this time taking four parameters, with two being integers, one being a floating point value and one a string.

do
	myFunction ( 10, 20, 3.671, "hello function" )
	
sync ( ) loop
function myFunction ( a as integer, b as integer, c as float, d as string ) print ( a * b / c ) print ( d ) endfunction

The same function could be rewritten using shortcuts to be displayed as.

function myFunction ( a, b, c#, d$ )
	print ( a * b / c# )
	print ( d$ )
endfunction

Both functions are exactly the same aside from the way in which they declare the variables. Whichever approach you use is down to personal preference. Some people prefer to be very explicit and use the as keyword to define their data types, while others like to use symbols such as # and $ to highlight the contents of their data.