a function return 2 values

Post Reply
User avatar
Silvio.Falconi
Posts: 7133
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

a function return 2 values

Post by Silvio.Falconi »

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
ukoenig
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany
Contact:

Re: a function return 2 values

Post by ukoenig »

Silvio,

a RETURN with many values
a INI-sample :

Code: Select all | Expand


// 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
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: a function return 2 values

Post by James Bott »

1) As Uwe said, return an array

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

Code: Select all | Expand

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
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: a function return 2 values

Post by Daniel Garcia-Gil »

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
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: a function return 2 values

Post by James Bott »

Daniel,

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

James
User avatar
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: a function return 2 values

Post by Enrico Maria Giordano »

James,

here it is:

Code: Select all | Expand

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
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: a function return 2 values

Post by James Bott »

Thanks Enrico.

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

James
User avatar
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: a function return 2 values

Post by Enrico Maria Giordano »

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
Silvio.Falconi
Posts: 7133
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: a function return 2 values

Post by Silvio.Falconi »

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
Enrico Maria Giordano
Posts: 8753
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Has thanked: 1 time
Been thanked: 4 times
Contact:

Re: a function return 2 values

Post by Enrico Maria Giordano »

Silvio,

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

EMG
Marc Vanzegbroeck
Posts: 1163
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium
Contact:

Re: a function return 2 values

Post by Marc Vanzegbroeck »

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
mercurial
Posts: 16
Joined: Wed Feb 04, 2015 2:26 pm

Re: a function return 2 values

Post by mercurial »

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...
Post Reply