passing string var as optional param to a function


Recommended Posts

Function xx(string file1, string file2, x, string xx)

private string xx ="?"

Private string something =file1

Private string something_else =file2

? xx +file1 +" " + file2

always prints "? file1 file2"

even if passed the optional argument as a string variable.

Is there a way to pass a string as an optional argument to a sequence function????

Link to comment
Share on other sites

which is the optional argument? xx? I think you are using a different language's syntax for this, or perhaps an older version of DF. If there's something still in the manual that says otherwise, please tell me.

Anyhow, with newer releases of DF, doing private string xx="?" will simply assign xx to "?" whether it is passed in or not. I think you want this:

if (isempty(xx))
   private string xx = "?"

Link to comment
Share on other sites

don't know what page, but this pulled from help, but it used a numeric var.

1) You want to create a function with optional arguments. In this case, you will want to put all the arguments in the function declaration, and then declare the optional ones as privates initialized with their default values. Note that all the optional arguments must be at the end of the argument list:

function IncrementBy(x, by)

private by = 1


In this example, IncrementBy(4) will return 5 because "by" is optional and defaults to 1. IncrementBy(4,2), however, will return 6 because "by" was specified.

Link to comment
Share on other sites

That is wrong and we're fixing it now. It was true back in 5.40 or so, but we changed it so that:

private x = 1

will set x to 1 whether x was declared or not. It used to be that x would only be set to 1 if x wasn't declared, which is why it used to work. Use the new method I showed before.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.