0
votes

What is Dynamic function in progress 4gl?I have tried many but I don't get clear understanding.Please explain with an example. I am the bigginner for this language.

1
An actual example of one or two of the many that you have tried might help to highlight what it is that you find unclear. - Tom Bascom

1 Answers

3
votes

Dynamic function is a way of dynamically invoking a function call. The compiler will have limited knowledge of what you are up to so you will get run time errors if you don't use correct inputs and outputs.

One example that runs different functions depending on user input:

DEFINE VARIABLE iFunc   AS INTEGER NO-UNDO.
DEFINE VARIABLE iReturn AS INTEGER NO-UNDO.

FUNCTION f1 RETURNS INTEGER :
    RETURN 1.        
END FUNCTION.

FUNCTION f2 RETURNS INTEGER :
    RETURN 2.        
END FUNCTION.

FUNCTION f3 RETURNS INTEGER :
    RETURN 3.        
END FUNCTION.

FUNCTION exp RETURNS INTEGER (INPUT piInt AS INTEGER):

    RETURN piInt * piInt.

END FUNCTION.

REPEAT :
    UPDATE iFunc LABEL "What function?".

    IF iFunc < 1 OR iFunc > 3 THEN LEAVE.
    MESSAGE "Return value: " DYNAMIC-FUNCTION ("f" + STRING(iFunc)) VIEW-AS ALERT-BOX.
END.

Another example where the function gets an input:

FUNCTION exp RETURNS INTEGER (INPUT piInt AS INTEGER):

    RETURN piInt * piInt.

END FUNCTION.


MESSAGE "10 x 10 = " DYNAMIC-FUNCTION ("exp", 10) VIEW-AS ALERT-BOX.

This call to the same "exp" function will compile but crash in runtime. The code is sending a string ("HELLO") as input where the function expects an integer.

MESSAGE "10 x 10 = " DYNAMIC-FUNCTION ("exp", "HELLO") VIEW-AS ALERT-BOX.