Blitz:Procedures

From Amiga Coding
Revision as of 16:59, 23 July 2015 by Daedalus (talk | contribs) (Declaring Statements)
Jump to: navigation, search

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

Declaring Functions