Difference between revisions of "Blitz:Procedures"

From Amiga Coding
Jump to: navigation, search
Line 35: Line 35:
  
 
==Calling Procedures==
 
==Calling Procedures==
 +
Procedures are called similarly to the built-in commands. To call a procedure, just enter it as you would a built-in command, remembering to include the curly brackets after the procedure name. To call the example statement above:
 +
<code>PrintWelcome{"Steve", 3}</code>
 +
This will produce the following output:
 +
<code>Welcome Steve
 +
You have 3 message(s) waiting</code>
 +
 +
Function procedures, like the built-in procedures, need something to do with their return value. To use the example above:
 +
<code>NPrint "The sum of 3 and 5 is ", AddUp{3, 5}</code>
 +
This will produce the following output:
 +
<code>The sum of 3 and 5 is 8</code>
 +
 +
 +
==Local Variables==

Revision as of 18:00, 23 July 2015

Blitz Basic / AmiBlitz supports the concept of procedures, which are like separate "modules" in your code that can be treated like statements or functions. In effect, this allows you to create your very own commands for situations where suitable commands don't exist in the standard Blitz libraries. Using procedures and functions helps keep your code tidy and easy to read, which in turn helps to avoid introducing bugs into your code.


Using Procedures

Two types of procedures are available: Statements and Functions. Similarly to the built-in statements and functions, these are more or less the same except that statements don't return a value whereas functions do. They can be used anywhere in you code *after* their position in the source, so it's generally a good idea to have them near the top. Using procedures is similar to using built-in commands, however there are a couple of differences:

  • Procedures always curly brackets around their arguments, for example:
DrawMySprite{x,y}
  • Procedures must have the curly brackets after their name, even if there are no arguments. For example:
ClearBoard{}


Declaring Statements

To create a statement, the Statement...End Statement commands are used, with the statement code in between. Curly brackets are required after the Statement, even if arguments are not required. For example: Statement ShowMyMessage{}

 NPrint "This is my first statement!"

End Statement This statement will show the message every time it is called in your code. Up to 6 arguments can be added in the curly brackets, separated by commas: Statement PrintWelcome{name$, messagecount}

 NPrint "Welcome ", name$
 NPrint "You have ", messagecount, " message(s) waiting"

End Statement Note: It is important that the data types of the arguments passed match what the code in your statement expects. To enforce proper variable type matching, use Syntax 2 near the top of your code.


Declaring Functions

Functions are declared similarly to statements, however they require an extra line of code in order to provide the return value. The command Function Return value is used to exit the function and return the value. Value can be any primitive type of information, and can be a constant or a variable. For example: Function AddUp{a, b}

 result = a + b
 Function Return result

End Function This function will return the sum of the two numbers provided as arguments when it is called. Note: It is important to ensure that the data type that is returned by the function is what is expected by your code. Returning a string is going to cause problems if your main code tries to use the result in a maths operation! To enforce proper variable type matching, use Syntax 2 near the top of your code.


Calling Procedures

Procedures are called similarly to the built-in commands. To call a procedure, just enter it as you would a built-in command, remembering to include the curly brackets after the procedure name. To call the example statement above: PrintWelcome{"Steve", 3} This will produce the following output: Welcome Steve You have 3 message(s) waiting

Function procedures, like the built-in procedures, need something to do with their return value. To use the example above: NPrint "The sum of 3 and 5 is ", AddUp{3, 5} This will produce the following output: The sum of 3 and 5 is 8


Local Variables