FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour a function return 2 values
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
a function return 2 values
Posted: 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 March-April 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
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: a function return 2 values
Posted: Sat Jan 31, 2015 01:12 PM
Silvio,

a RETURN with many values
a INI-sample :

Code (fw): Select all Collapse
// 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.
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: a function return 2 values
Posted: Sat Jan 31, 2015 06:06 PM
1) As Uwe said, return an array

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

Code (fw): Select all Collapse
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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: a function return 2 values
Posted: 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

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: a function return 2 values
Posted: Sun Feb 01, 2015 05:27 PM

Daniel,

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

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: a function return 2 values
Posted: Sun Feb 01, 2015 05:54 PM
James,

here it is:

Code (fw): Select all Collapse
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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: a function return 2 values
Posted: Sun Feb 01, 2015 05:58 PM

Thanks Enrico.

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

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: a function return 2 values
Posted: Sun Feb 01, 2015 06: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
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: a function return 2 values
Posted: Mon Feb 02, 2015 09: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 March-April 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
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: a function return 2 values
Posted: Mon Feb 02, 2015 09:50 AM

Silvio,

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

EMG

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: a function return 2 values
Posted: Mon Feb 02, 2015 02: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
Posts: 16
Joined: Wed Feb 04, 2015 02:26 PM
Re: a function return 2 values
Posted: Wed Feb 04, 2015 05: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...

Continue the discussion