PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: Jeffrey Ferreira on February 25, 2019, 11:51:13 AM

Title: Defining Function
Post by: Jeffrey Ferreira on February 25, 2019, 11:51:13 AM
Hello List,

I'm trying to avoid using a global function but I would like function available in multiple programs.  I tried doing the following: 

PROG01

10 perform "PR0G02;DEFINE_FUNCTIONS"
20 PRINT fnPAIR$("Key","Value")

but I can't access the function defined in PR0G02

is my only recourse  a Global Function?

jeff
Title: Re: Defining Function
Post by: James Zukowski on February 25, 2019, 11:57:20 AM
You could look into XEQ as a possibility:
https://manual.pvxplus.com/PXPLUS/functions/xeq.htm (https://manual.pvxplus.com/PXPLUS/functions/xeq.htm)
Title: Re: Defining Function
Post by: Jeffrey Ferreira on February 25, 2019, 12:54:59 PM
This looks promising James. I will give it a try. Thank you.
Title: Re: Defining Function
Post by: Devon Austen on February 25, 2019, 03:40:20 PM
Hi Jeff,

You could make it an object with the functions defined as methods and call it something like myUtilities.

Code: [Select]
DEF CLASS "myUtilities"
 FUNCTION pair$(key$, value$)pair
 END DEF
 pair:
 ENTER key$, value$
...
return result$

Then in programs you want to use it you do a myUtilies=NEW("myUtilties"). Then you simply call the functions like myUtilities'pair$("Key","Value")
Title: Re: Defining Function
Post by: Mike King on February 26, 2019, 09:29:29 AM
Simply make your function global by the inclusion of the % in the function name as in:

DEF FN%PAIR$(local a$, local b$)
....
END DEF

-or-

DEF FN%PAIR$(local a$, local b$)=A$+"="+quo+b$+quo

See the description on "Global User-defined Functions" at the bottom of this page:
https://manual.pvxplus.com/page/PxPlus%20User%20Guide/Programming%20Constructs/Called%20Procedures/Overview.htm