In the original D++ language, programs had to follow a strict order of code. You could call built-in functions, such as instr(), but you couldn't write your own. The changed with the introduction of user functions in D++ version 3.0. While you still can write code this way, using your own functions gives you much more power.
When the program loads, D++ dynamically scans the source code and parses the individual functions, sorting through the parameters and code of the functions. D++ then calls the main() function. main() is a special function that must be defined, but you cannot call main() elsewhere in your code, nor can you give main() any arguments. After that, D++ does not even look at any of the other functions unless they are called from main() or a susbsiquent procedure.
Declaration
The declaration of a function is very simple. It is similar to more C++ style code.
function name(arguments)
{
statements
}
The function name, like any other identifier in D++, must be a valid name - only alphanumeric characters, hyphens (-) and underscores (_). You can declare as many arguments as you want, or none at all. The arguments should be separated by commas. Be sure that when you are call the function elsewhere in the program that you also give it the same amount of arguements, or else a run-time error will occur.
Calling Functions/Return Value
Functions can be called just like any other function. You can either use them in an expression if you want a value returned or you can call the function by itself. In an expression, you might havex = myFunction(a,b); the result of myFunction would be assigned to x. To call a function on it's own, simple write the function name and parenthesis: myFunction(a,b); Note that you MUST use parenthesis when calling the function, even if the function has no parameters - this is how D++ recognizes the statement as a function.
Returning a value from the function can be done in a variety of ways. Both VB and C++ programmers will be happy here, as either method is usable. Following the VB method, you can assign a value to the function. For example, myFunction = 3; would return a value of 3, when the function ends. The function itself can act like any other variable. Following the C++ method, you can write return 3; which will assign the function a value of 3, then exit the function. Remember this important difference - the assignment method will not exit the function, while the return method will. Additionally, if you just want to exit the function, you can use either the exit_function; or just return;.
Available Preset Functions
There are several functions available for use in D++ which you can use at your option. At a certain point in execution, these functions are called, and you can optionally add code to handle these events in a way that better fits with your program. You can't call them within your code, however. Let's look at these functions.
- function ~main() { }
~main() works in the opposite way ~main does. It is executed when the program ends, rather then at the beginning. It will either execute when the program is "finished", or when the X button is clicked - whichever comes first. It takes no arguments.
- function error(type, text, proc, char) { }
error() can occur instead of the normal runtim-error dialog box. If this function is not found, then the normal dialog will appear. Otherwise, the function has 4 variables - type, text, proc, and char. "Type" is the type of error - either a syntax error, runtime error, or RPN error. "text" is a short description of the error. "proc" is the name of the function that the error occured in, and "char" is the character where the error occured.
Back to Documentation Index