a function return 2 values
- Silvio.Falconi
- Posts: 7133
- Joined: Thu Oct 18, 2012 7:17 pm
- Been thanked: 1 time
a function return 2 values
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
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
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
Re: a function return 2 values
Silvio,
a RETURN with many values
a INI-sample :
best regards
Uwe![Smile :)](./images/smilies/icon_smile.gif)
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
![Smile :)](./images/smilies/icon_smile.gif)
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.
i work with FW.
If you have any questions about special functions, maybe i can help.
- James Bott
- Posts: 4840
- Joined: Fri Nov 18, 2005 4:52 pm
- Location: San Diego, California, USA
- Contact:
Re: a function return 2 values
1) As Uwe said, return an array
2) And my preferred method, use a class with two data objects.
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
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
- Daniel Garcia-Gil
- Posts: 2365
- Joined: Wed Nov 02, 2005 11:46 pm
- Location: Isla de Margarita
- Contact:
Re: a function return 2 values
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
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
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
- James Bott
- Posts: 4840
- Joined: Fri Nov 18, 2005 4:52 pm
- Location: San Diego, California, USA
- Contact:
Re: a function return 2 values
Daniel,
I don't really understand how that works. Can you provide a complete working example?
James
I don't really understand how that works. Can you provide a complete working example?
James
- 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
James,
here it is:
EMG
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
- James Bott
- Posts: 4840
- Joined: Fri Nov 18, 2005 4:52 pm
- Location: San Diego, California, USA
- Contact:
Re: a function return 2 values
Thanks Enrico.
I am getting the feeling that is how classes work behind the scenes?
James
I am getting the feeling that is how classes work behind the scenes?
James
- 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
James,
Probably that's the concept. But hashes are implemented at C language level in xHarbour, not at PRG one.
EMG
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
- Silvio.Falconi
- Posts: 7133
- Joined: Thu Oct 18, 2012 7:17 pm
- Been thanked: 1 time
Re: a function return 2 values
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
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
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
- 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
Silvio,
@ as parameter suffix means "by reference". [x]Harbour fully support it, just like Clipper.
EMG
@ as parameter suffix means "by reference". [x]Harbour fully support it, just like Clipper.
EMG
-
- Posts: 1163
- Joined: Mon Oct 17, 2005 5:41 am
- Location: Belgium
- Contact:
Re: a function return 2 values
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
FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Re: a function return 2 values
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...
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...