a function return 2 values

a function return 2 values

Postby Silvio.Falconi » Sat Jan 31, 2015 12:31 pm

I have a function and I wish have the value of return and another value it is possible ?
sample :

If !GIORNOLIB(nGiorno, @nOre, nOra)


How i can have also nOre ?
to use then to another function sample
IF calsslib(ngiorno,nore,nora)


...

the function I found on old clipper app I must converte

static function GIORNOLIB(nGiorno, nOre, nOra)
Local lreturn := .f.

if (Val(do->giornoli) == nGiorno .AND. nOra == 1 .AND. ;
do->punti < "3" .AND. Val(do->giornoli) == nGiorno ;
.AND. nOra == 2 .AND. do->punti < "2")

nOre:= iif(Left(da->parame, 1) == "0", 10, Val(Left(da->parame, 1)))

lreturn := .T.
endif
return lreturn
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7069
Joined: Thu Oct 18, 2012 7:17 pm

Re: a function return 2 values

Postby ukoenig » Sat Jan 31, 2015 1:12 pm

Silvio,

a RETURN with many values
a INI-sample :

Code: Select all  Expand view  RUN

// the INI returns a ARRAY
aValues := GET_INI(cWorkFile, nTxtColor)

cWorkFile := aValues[1] // from INI
nTxtColor := aValues[2] // from INI
...
...
// --------- INI - Read -------------

FUNCTION GET_INI( cWorkFile, nTxtColor )
Local oIni, cIniFile := c_path + "PROJECT.INI"

IF !FILE ( c_path + "PROJECT.ini" ) // creates the INI if not exists
    SAVE_INI( cWorkFile, nTxtColor )
ELSE
    INI oIni FILE c_path + "PROJECT.INI"
                 cWorkFile := GetPvProfString( "System",            "Image",        "Picture1.jpg" , cIniFile )
             nTxtColor :=  Val(GetPvProfString( "System",       "Textcolor",    "0" , cIniFile ) )
    ENDINI
ENDIF

RETURN { cWorkFile, nTxtColor } // return-values = ARRAY
 


best regards
Uwe :)
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: a function return 2 values

Postby James Bott » Sat Jan 31, 2015 6:06 pm

1) As Uwe said, return an array

2) And my preferred method, use a class with two data objects.

Code: Select all  Expand view  RUN
class TWhatever
   Data value1
   Data value2
endclass

Method new(...)
   ::value1:=1
   ::value2:=2
return self

oObj:=TWhatver():new()

msgInfo( oObj:value1 )
msgInfo( oObj:value2 )

 

3) If the calling program is in the same prg as the function you could use file-wide static vars

I highly recommend number 2, the class method.

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: a function return 2 values

Postby Daniel Garcia-Gil » Sun Feb 01, 2015 12:48 pm

Hello

I prefer use hash, example

h = any()

You can access h[ "var1" ], h[ "var2" ]

Functions any()

Local hVar = {=>}

...

hVar [ "var1" ] = 1234
hVar [ "var2" ] = "abcd"

Return hVar
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita

Re: a function return 2 values

Postby James Bott » Sun Feb 01, 2015 5:27 pm

Daniel,

I don't really understand how that works. Can you provide a complete working example?

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: a function return 2 values

Postby Enrico Maria Giordano » Sun Feb 01, 2015 5:54 pm

James,

here it is:

Code: Select all  Expand view  RUN
FUNCTION MAIN()

    LOCAL hHash := MyFunc()

    ? hHash[ "Val1" ], hHash[ "Val2" ]

    RETURN NIL


STATIC FUNCTION MYFUNC()

    LOCAL hHash := { => }

    hHash[ "Val1" ] = 123
    hHash[ "Val2" ] = 456

    RETURN hHash


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: a function return 2 values

Postby James Bott » Sun Feb 01, 2015 5:58 pm

Thanks Enrico.

I am getting the feeling that is how classes work behind the scenes?

James
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: a function return 2 values

Postby Enrico Maria Giordano » Sun Feb 01, 2015 6:06 pm

James,

James Bott wrote:Thanks Enrico.

I am getting the feeling that is how classes work behind the scenes?


Probably that's the concept. But hashes are implemented at C language level in xHarbour, not at PRG one.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: a function return 2 values

Postby Silvio.Falconi » Mon Feb 02, 2015 9:31 am

thanks to all
but On old clipper app I have this command

If Giornolib(ngiorno,@nOre,nOra)

then the value of @nOre id used by another function


the Giornolib function is :

static function GIORNOLIB(ngiorno, nOre, nOra)
if (Val(anagrafe->giornoli) == ngiorno .AND. nOra == 1 .AND. ;
anagrafe->punti < "3" .AND. Val(anagrafe->giornoli) == ngiorno ;
.AND. nOra == 2 .AND. anagrafe->punti < "2")
nOre:= iif(Left(datis->parame, 1) == "0", 10, ;
Val(Left(datis->parame, 1)))
return .F.
endif
return .T.


I not Know what I made on 1992 but the app on Dos run ok
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7069
Joined: Thu Oct 18, 2012 7:17 pm

Re: a function return 2 values

Postby Enrico Maria Giordano » Mon Feb 02, 2015 9:50 am

Silvio,

@ as parameter suffix means "by reference". [x]Harbour fully support it, just like Clipper.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8718
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: a function return 2 values

Postby Marc Vanzegbroeck » Mon Feb 02, 2015 2:32 pm

Silvio.Falconi wrote:thanks to all
but On old clipper app I have this command

If Giornolib(ngiorno,@nOre,nOra)

then the value of @nOre id used by another function


the Giornolib function is :

static function GIORNOLIB(ngiorno, nOre, nOra)
if (Val(anagrafe->giornoli) == ngiorno .AND. nOra == 1 .AND. ;
anagrafe->punti < "3" .AND. Val(anagrafe->giornoli) == ngiorno ;
.AND. nOra == 2 .AND. anagrafe->punti < "2")
nOre:= iif(Left(datis->parame, 1) == "0", 10, ;
Val(Left(datis->parame, 1)))
return .F.
endif
return .T.


I not Know what I made on 1992 but the app on Dos run ok


Silvio,

That is also working in FWH
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: a function return 2 values

Postby mercurial » Wed Feb 04, 2015 5:12 pm

Hi

Functions can return only one value. This value can be of any supported type so you can simulate multiple return values using array, hashes, classes.

Or you can use parameters passed by reference:

If Giornolib(ngiorno,@nOre,nOra)

In this call nGiorno and nOra is passed by value (the actual values of nGiorno and nOra are passed to the function) while nOre is passed by reference (the "address" of the variable is passed to the function).

Infact in the function GiornoLib( nGiorno, nOre, nOra ) // Note that in function definition you can't specify @
there is this instruction:
nOre:= iif(Left(datis->parame, 1) == "0", 10, Val(Left(datis->parame, 1)))

A new value to variable nOre is assigned: you can check that after the IF the variable has changed its value.

So your question "How i can have also nOre ?" is easily answered: you already have nOre "returned"... actually assigned and not returned...

PS: note that boolean, date, chars, numerics are always passed by value (unless @ is specified). "Complex" objects like classes, arrays, hashes etc are always passed by reference (you can modify them in the function)
PPS: the fact that the variable names in GiornoLib are the same as in the calling ones is casual...
mercurial
 
Posts: 16
Joined: Wed Feb 04, 2015 2:26 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 56 guests