Self Contained Fuctions

Blitz3D Forums/Blitz3D Beginners Area/Self Contained Fuctions

One way to make managing dependancies easier (though not automatic) is to use If statements that take a constant expression as a parameter to control whether code is compiled or not. Blitz will collapse these If statements down so that only the parts of the code that can ever be run will actually be compiled.

For example, this code:-

Graphics 800, 600, 0, 2
Const RELEASE_VERSION = True
If RELEASE_VERSION
	do_this = True
Else
	do_this = False
EndIf
Print do_this
WaitKey
End


Compiles exactly the same as this code:-

Graphics 800, 600, 0, 2
do_this = True
Print do_this
WaitKey
End


As verified by this checksumming program:-

; Prints a checksum of the test program file, which should be compiled as an executable called 'test.exe' and should be in the same folder as this program.


Graphics 800, 600, 0, 2

the_filepath$ = "test.exe"

the_file = ReadFile ( the_filepath$ )

If the_file ; The file was successfully opened for reading.

	the_filesize = FileSize ( the_filepath$ )

	data_buffer_bank_handle = CreateBank ( the_filesize )

	ReadBytes data_buffer_bank_handle, the_file, 0, the_filesize ; Load the data bytes from the file into the buffer bank.

	CloseFile the_file

		 ; -- Calculate a checksum for the loaded file data.
		the_max_pos = the_filesize - 1
		For i = 0 To the_max_pos
			the_checksum = the_checksum + PeekByte ( data_buffer_bank_handle, i )
		Next
		;^^^^^^

Else ; The file could not be opened for reading.

	Print "UNABLE TO OPEN FILE ERROR"

EndIf

Print the_checksum

WaitKey

End