Page 1 of 1 1
Topic Options
#21131 - 2002-05-03 10:46 AM Using Functions
briannz556 Offline
Getting the hang of it

Registered: 2002-05-03
Posts: 64
Loc: new zealand
My very first posting so perhaps everyone knows this. Anyway, here goes.

I want to use functions very much like procedures whereby I don't use the return value. I discovered by trial and error that a call to a function like:

;lines of code
initialiseparameters()
;more lines of code

can cause problems at times. Is this an obvious statement? I have a sample piece of code to illustrate if necessary.

Top
#21132 - 2002-05-03 10:50 AM Re: Using Functions
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Welcome Brian,

not quite obvious ... please post your sample code

Jochen
_________________________



Top
#21133 - 2002-05-03 10:52 AM Re: Using Functions
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Or do you mean you want to suppress the console output that your functions generate ?

Try to use a nulifier like :

$nul = function()

hth

Jochen
_________________________



Top
#21134 - 2002-05-04 12:20 AM Re: Using Functions
briannz556 Offline
Getting the hang of it

Registered: 2002-05-03
Posts: 64
Loc: new zealand
Ok, I seem to have destroyed my sample. So perhaps I'll start again by asking a question. Why won't the following concept work?

break on
DIM $PCsetting[4]
InitialiseVariables()
Report()
Quit

FUNCTION InitialiseVariables()
$PCsetting[1]='one'
$PCsetting[2]='two'
$PCsetting[3]='three'
ENDFUNCTION

FUNCTION Report()
FOR EACH $element IN $PCsetting
?$element
NEXT
ENDFUNCTION

Top
#21135 - 2002-05-04 12:32 AM Re: Using Functions
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
It's all down to scope.

The following is a better solution as it avoids exposing the variables in the function:
code:
Break ON 

Report(InitialiseVariables())

Function InitialiseVariables()
Dim $PCsetting[4]
$PCsetting[1]='one'
$PCsetting[2]='two'
$PCsetting[3]='three'
$InitialiseVariables=$PCSetting
EndFunction

Function Report($PCSetting)
For Each $element In $PCsetting
$element ?
Next
EndFunction

If you really need the array to have a global scope then the following works:
code:
Break ON 

GLOBAL $PCSetting
$PCSetting=InitialiseVariables()
Report()

Function InitialiseVariables()
Dim $PCsetting[4]
$PCsetting[1]='one'
$PCsetting[2]='two'
$PCsetting[3]='three'
$InitialiseVariables=$PCSetting
EndFunction

Function Report()
For Each $element In $PCsetting
$element ?
Next
EndFunction

Don't forget the array subscript starts at zero, not one.

[ 03 May 2002, 12:36: Message edited by: Richard Howarth ]

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 581 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.057 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org