execute
execute
PROGRAM
,
PARAMETERS

The execute statement allows a procedure to call a sequence of statements within the current procedure as a “mini-subroutine”.

Parameters

This statement has two parameters:

program – is the text (source code) of the program you want to run.

parameters – is a list of parameters to be passed to the subroutine. This list is optional, you don’t have to pass any parameters if you don’t need to. See parameter( and setparameter for more information on accessing parameters within a subroutine.

Description

This statement allows a program to build a “mini-procedure” on the fly, and then run that procedure as a subroutine. The text of the “mini-procedure” can be in a field, a variable, or constructed using a formula. You can use the if error statement (see Error Handling) to check for syntax errors in the mini-program. Run time errors (missing variable, type mismatch, etc.) are handled just as they would be for any other program. You cannot use the debugger (single step, etc.) on a mini-procedure. After the mini-procedure has finished the main program continues from the next statement, just like a subroutine (unless the mini-procedure contains a stop statement).

If there is a syntax error in the mini-program (misspelled command, unexpected formula operator, etc.) two special local variables will be created: ExecuteErrorStart and ExecuteErrorEnd. These two variables contain the numbers for the starting and ending position of the error (the number of characters from the start of the mini-program.) The procedure can also find out the exact error with the info(“error”) function.

The example below illustrates how the execute statement works, but is rather silly since the mini-program could simply have been included in the main program. The real power of this statement is the ability to generate the mini-program on the fly, in response to changing circumstances. You could even keep mini-programs in a database field, giving each record its own program!

    local myProgram
    myProgram=
        {if clipboard() beginswith "Error" }+
        {message clipboard() }+
        {endif }
    execute myProgram

If you want to check the mini-program for syntax errors without running it, put a return at the beginning of the mini-program. The return statement will prevent the mini-program itself from executing. This example assumes that the mini-program is in a field called Source. If there is an error, it displays the error and selects the offending section of the program (this assumes that Source is being edited in a Text Editor Object.) The -4 compensates for the return statement and space.

    execute "return "+Source
    if error
       message "Mini-program contains an error: "+info("error")
       activeobjectaction "SetSelection",ExecuteErrorStart-4,ExecuteErrorEnd-4
    else
       message "Mini-program is A-OK"
    endif

#### Parameters

When you call a subroutine you can supply one or more parameters to be passed to the subroutine (each parameter must be separated by a comma). When using the execute statement the parameters are listed after the source code, like this:

    execute source,parameter1,parameter2,parameter3, ... parameterN

Within the source code you can access the parameters with the parameter( function, or change the value of a parameter with the setparameter statement. This is especially useful for accessing the main program’s local variables within the “mini-procedure”. Normally the local variables in the main program cannot be accessed within the “mini-procedure”, but parameters provide a way to do this. Here is a super simple example that shows how a parameter can be accessed.

    local x,y,z,xs
    x=3 y=2 z=5
    xs={message parameter(1)}
    execute xs,x
    execute xs,y
    execute xs,z

(Of course this example is rather contrived – there is no reason to not simply use the message statement directly instead of using the execute statement.)

You can also use the setparameter statement to modify the value of a parameter passed to the mini-procedure.

Note: Another way to access local variables in the main program is to use the executelocal statement.

See Also

- call -- allows a procedure to call a separate procedure within the current database as a subroutine.
- call( -- allows a procedure to be called as a subroutine within a formula and return a result.
- calledby( -- returns true if this function is in code called by the specified database, the specified procedure, or both.
- callingdatabase( -- returns the name of the database that called this procedure as a subroutine, if any.
- callingprocedure( -- returns the name of the procedure that called this procedure as a subroutine, if any.
- callwithin -- allows a procedure to call a mini-procedure within a separate procedure within the current database as a subroutine.
- callwithin( -- allows a "mini-procedure" to be called as a subroutine within a formula and return a result.
- callwithindatabase( -- returns true if the current procedure was called by another procedure in the same database, false if it was called by a procedure in another database.
- Custom Statements -- creating your own custom statements that can be used in your code.
- execute( -- allows a formula to execute a sequence of statements and return a result.
- executeasap -- executes the specified code at the first possible opportunity, non-atomically.
- executecatcherrors -- is the same as the execute statement, except for the fact that if an error occurs while running, it can be trapped by an if error statement immediately following the executecatcherrors statement.
- executelocal -- is the same as the execute statement, but it shares local variables with the procedure that called it.
- farcall -- allows a procedure to call a separate procedure within a different database as a subroutine.
- farcallwithin -- allows a procedure to call a mini-procedure within a separate procedure in a different database as a subroutine.
- getproceduretext( -- gets the contents (source code) of a procedure.
- info("parameters") -- returns the number of parameters passed to a subroutine.
- info("procedurestack") -- returns the contents of Panorama's procedure call stack.
- parameter( -- is used to transfer data between a main procedure and a subroutine.
- parameterentity( -- returns the entity (field or variable) associated with a subroutine parameter, if any.
- return -- ends a subroutine.
- returnerror -- passes an error back to the current subroutines calling procedure.
- setparameter -- is used to transfer data from a subroutine back to the main procedure that called it.
- shortcall -- allows a procedure to call a sequence of statements within the current procedure as a "mini-subroutine".

History

Version | Status | Notes
10.0 | Updated | Carried over from Panorama 6.0 but now allows parameters to be passed to and from the subroutine using the param( function and setparameter statement.