Phantom Documentation

[Help Home] [Phantom Home]
User Defined Functions

Phantom allows for user defined functions. User defined functions are functions whose characteristics are determined by the user. The functions are made up of other Phantom statements or commands, or of other user defined functions. User defined functions can have any number of input parameters, but can only return one value. Note that variables declared inside a function are local to that function.


  function type Name(type1 Parameter1, type2 Parameter2,...)
  
{
    type Ret;
    Statement1;
    Statement2;
    .
    .
    
return Ret;
  
}


Where,

type - The return type of the function.
Name - The Name of the function.
type1 Parameter1, type2 Parameter2,... - The variable types and names of the input parameters. The names are local to the function, and are used by the function to refer to the input variable.
Statement1, Statement2... - Commands and operations to be performed by the function. This can be any Phantom command or operation, or another user defined function.
Ret - The return value of the function.


Notice that the function must include the return statement to indicate the exit point of the function. This return statement is followed by the name of the variable to be returned, if any. Also note that all variables are local to the function unless returned via the return statement. Any variable type may be passed to or returned from a function.


To use a function, simply write the function's name, with the parameters, and provide a return value place holder exactly like any built-in Phantom function.


# Function declaration
function string DisplayValues(int i, real r, bool b){
  string s = “Values: ”;
  s = s + string(i) + “,”;
  s = s + string(r) + “,”;
  s = s + string(b);
  return s;
}

# Call the function
string s = DisplayValues(10, 20.0, true);
disp(s);


The function declaration and definition can be either in a separate script file or in the same script file where it is used. If it is in a separate file, that file must be accessed using the include statement before the function is called. If it defined in the same file where it is called, it must be declared and defined before the function is used. The exception to this is for multiple functions defined in a single file. A function defined in the file can call a function defined later in the file, as long as the function is not called before the second function is defined.

Note that Phantom can have multiple functions with the same name, as long as the parameter lists are different.

Sample Functions File (functions.txt)

# Function declarations
function int Sum(int i1, int i2){
  return Add(i1, i2); # Can call a function defined later
}

function int Add(int i1, int i2){
  return i1 + i2;
}


Sample script.txt file that calls functions.txt functions:

include "functions.txt"; # Accesses the functions file

# Displays the names of the loaded user-defined functions
what("User Defined");

# Call the function
int j = Sum(10, 20);

disp(j);

See Also: return

 


Copyright 2000-2011 Phantom Automated Solutions