How to write functions with optional arguments?

Is it possible to write functions with optional arguments? I want to create a function similar to the inbuilt rndu function which has an optional argument state.

1 Answer



0



Yes, it is possible to write procedures which use optional arguments. The current API was written for internal use, so it is not very pretty. However, in the fall of 2019, a more user-friendly version will be available.

In the mean-time, here is a basic example.

print myRndu(1, 1, 777);
print myRndu(1, 1);
print myRndu(1, 1, 777);

#include dynargs.dec
// The optional argument is specified by ...
// it must be the last input to the procedure
proc (1) = myRndu(r, c, ...);
    local n_dynargs, x, seed, state;

    // Find out how many inputs came
    // in as ...
    n_dynargs = COUNT_DYNARGS;

   if n_dynargs == 0;
       x = rndu(r, c);
   elseif n_dynargs == 1;
       // The 1 after the comma below is
       // the index of the dynamic / optional argument
       seed = sysstate(GET_ONE_DYNARG, 1);
       { x, state } = rndu(r, c, seed);
   endif;

   retp(x);
endp;

If you run the above code you will get something like this:

      0.69992051
      0.13141210
      0.69992051

The first and last number will be the same, but the middle number will be different.

aptech

1,773

Your Answer

1 Answer

0

Yes, it is possible to write procedures which use optional arguments. The current API was written for internal use, so it is not very pretty. However, in the fall of 2019, a more user-friendly version will be available.

In the mean-time, here is a basic example.

print myRndu(1, 1, 777);
print myRndu(1, 1);
print myRndu(1, 1, 777);

#include dynargs.dec
// The optional argument is specified by ...
// it must be the last input to the procedure
proc (1) = myRndu(r, c, ...);
    local n_dynargs, x, seed, state;

    // Find out how many inputs came
    // in as ...
    n_dynargs = COUNT_DYNARGS;

   if n_dynargs == 0;
       x = rndu(r, c);
   elseif n_dynargs == 1;
       // The 1 after the comma below is
       // the index of the dynamic / optional argument
       seed = sysstate(GET_ONE_DYNARG, 1);
       { x, state } = rndu(r, c, seed);
   endif;

   retp(x);
endp;

If you run the above code you will get something like this:

      0.69992051
      0.13141210
      0.69992051

The first and last number will be the same, but the middle number will be different.


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy