Question How To Define Function With Optional Parameter?

Ferry Huang

New Member
could anyone give example on how to define function with optional parameter?
for example TRIM function has optional parameter as below syntax
TRIM ( expression [ , trim-chars ] )

MESSAGE TRIM(" abc ").
MESSAGE TRIM("abca", "a").

above code will result in (without quotes)
"abc"
"bc"

thanks.
 

mollyfud

Member
Hmmm... Don't think that User Defined Functions can have optional parameters!
Depending on the version you are using, could you use methods of a class with multiple signatures/overloads?

Something like this (Note: Example is a trivial one to prove the point):
-------------------testOverload.cls-----------------------------
USING Progress.Lang.*.

BLOCK-LEVEL ON ERROR UNDO, THROW.

CLASS testOverload:

/*------------------------------------------------------------------------------
Purpose:
Notes:
------------------------------------------------------------------------------*/

METHOD PUBLIC character myTrim(p1 as char ):
DEFINE VARIABLE result AS CHARACTER NO-UNDO.
result = trim(p1).

RETURN result.

END METHOD.

METHOD PUBLIC character myTrim(p1 as char, p2 as char ):
DEFINE VARIABLE result AS CHARACTER NO-UNDO.
result = trim(p1,p2).

RETURN result.

END METHOD.

END CLASS.
==============
And called like this:
------
define variable myt as class testOverload.
myt = new testOverload().
MESSAGE myt:myTrim(" abc ") myt:myTrim("abca", "a")
VIEW-AS ALERT-BOX.
=====

HTH
 

Ferry Huang

New Member
thanks for your help Molly,
but I am not using CLASS.
is it possible to define function with optional parameter without using CLASS?
or is it possible to create STATIC CLASS like in CPP so we can directly use the METHOD of that CLASS?
 

RealHeavyDude

Well-Known Member
Nope. The parameter signature of UDFs ( user defined function ) and procedures as well as internal procedures is resolved at compile time. There is no way to change the parameter signature at runtime. Furthermore a given UDF must be unique within the procedure - meaning you can't have two function with the same name but different parameter signatures. With methods ( in classes ) that limit is gone.

Nevertheless, you can use a temp-table as a parameter which can then hold as many records as you want to - you could use a single temp-table record for one parameter.

but I am not using CLASS.
Well, maybe you should.

Heavy Regards, RealHeavyDude.
 

TheMadDBA

Active Member
Also read the class based development documentation for your version of OE. Depending on your exact version you will have varying features available.

It isn't quite at the level of some other OO languages but in modern versions it is getting pretty close.
 

Rutger Olthuis

New Member
It's not a very chique solution, but if your input is limited to plain datatypes like characters, you could use a delimiter in the single input parameter.
For instance:
Code:
define variable inputString as character no-undo.
inputString = STRING(myVar1 + CHR(3) + myVar2).
if myFunction(inputString) = true then....
function myFunction returns logical (functionInput as character) :
   case num-entries(functionInput,CHR(3)):
   when 1 then do:
   end.
   when 2 then do:
   end.
   otherwise do:
        message "to many input parameters!".
   end.
end function.

Excuses if the code is not compiling, wrote it in the forum editor ;)
 

Ferry Huang

New Member
thanks Rutger, yes it will work.
but I'm looking the official optional parameter from progress language and it seems that they do not exist, lol.
 

TheMadDBA

Active Member
Well they do exist in the class based (ooabl). Not sure why you don't want to use oo for overloads. Works very well in my opinion.
 

RealHeavyDude

Well-Known Member
IMHO the OO capabilities of recent OpenEdge versions come pretty close. Sure there are things you can do in other language like Java that the ABL is at a first glance. Nevertheless, some capabilities ( like reflection for example or the lambdas in Java 8 ) are controversially discussed by OO purist as compromising the purity of OO.

Bottom line: There is no argument against using OO ABL except maybe the lack of knowledge. I do OO ABL ( and Java too ) now for years and it rocks! You can do really cool stuff with OO ABL.

Heavy Regards, RealHeavyDude.
 
Top