Page 1 of 1

Posted: Tue Dec 17, 2002 3:44 pm
by BackupUser
Restored from previous forum. Originally posted by JimmyClif.

Hello,

I'm just toying around a bit to see if I like Basic or not... Partwise I think it's extremly confusing... Like this:

szTemp$ = "Hello"
MyMsg szTemp$



sub MyMsg(temp)

temp = temp + "!"
MessageRequester(temp,temp,0)

end sub

(I don't have any manuals here so I have to guess the syntax a bit)

Here The MessageBox displays: Hello!
but upon return from the sub szTemp is still only "Hello"

Why is that? Am I doing something wrong?

Tia Jimmy

Posted: Tue Dec 17, 2002 4:31 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

> Here The MessageBox displays: Hello!
> but upon return from the sub szTemp is still only "Hello"

It appears that you want to append ! to the end of the variable that
is passed to MyMsg -- am I right? If so, it's done like this:

Code: Select all

Procedure.s MyMsg(temp$) ; [b].s[/b] = We're returning a string to main code.
  temp$=temp$+"!" ; temp$ initially holds value of var passed to it (szTemp$).
  MessageRequester("Inside Sub",temp$,0) ; Show value of temp$ to us.
  ProcedureReturn temp$ ; Return value of temp$ to main code.
EndProcedure
;
szTemp$="Hello" ; Initial string.
szTemp$=MyMsg(szTemp$) ; Send initial string to procedure.
MessageRequester("Outside Sub",szTemp$,0) ; Show initial string's new value.

Posted: Wed Dec 18, 2002 1:46 pm
by BackupUser
Restored from previous forum. Originally posted by JimmyClif.

Thanks a lot PB...

Having an assembler background I'm kind of used to have anything changed forever after I changed it once..

This confuses me though... What if I need to change 2 buffers in a procedure?

Code: Select all

Dim szTemp.s(255) ; Global [This means allocate 255 bytes as string here]
Dim szTemp2.s(255)

Procedure MyMsg.s(temp$,temp2$)
temp$ = temp$ + " First Buffer"
temp2$ = temp2$ + " Second Buffer"
;ProcedureReturn ?
EndProcedure

szTemp$ = "1"
szTemp2$ = "2"
MyMsg(szTemp,szTemp2)
God, I wish I wasn't at work :(

Posted: Wed Dec 18, 2002 2:16 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

> Having an assembler background I'm kind of used to have anything
> changed forever after I changed it once..

If you change a variable in PureBasic it also stays changed forever,
or at least until you change it again.

> Dim szTemp.s(255) ; Global [This means allocate 255 bytes as string here]

No, that means you've created an array of 255 individual strings named
szTemp. To create a global string of 255 bytes you could do it this way
in PureBasic:

Global szTemp$ : szTemp$=Space(255)

Note, however, that you don't really need to "pad" a string with
PureBasic to hold data, unless you're filling it with the return
data from a Win32 API call. So if you want to concatenate two
strings together, you don't need to firstly create a third string
of a length made up of the other two's lengths combined.

Any variables inside a procedure in PureBasic are not global, unless
you specifically declared them to be global (as shown above), and the
declaration is done before the procedure is created. As such, any
non-global-declared variables inside a procedure are local to that
procedure only, and contain null values whenever the procedure is
called. Also, as they are local to that procedure, you can use the
same variable name inside many different procedures with no clashes.

Posted: Wed Dec 18, 2002 2:36 pm
by BackupUser
Restored from previous forum. Originally posted by JimmyClif.

Cheers,

I think we made a giant step ahead here :wink: (well, for me at least)

>>If you change a variable in PureBasic it also stays changed forever,
>>or at least until you change it again.

..but why doesn't the "!" stay appended to "Hello" if I don't return it as value to the callee?

Cheers,
Jimmy

Posted: Wed Dec 18, 2002 3:35 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

> why doesn't the "!" stay appended to "Hello" if I don't
> return it as value to the callee?

In this code: temp$=temp$+"!" ? Because temp$ was not declared as
a global variable, and so temp$ in that procedure is destroyed when
the procedure finishes. Remember also that any non-global variables
in a procedure are always null when the procedure is called, unless
you pass the value of another variable to it when calling.

If you wanted temp$ in that procedure to retain its value, you'd have
to put the code Global temp$ before the procedure declaration (that
is, in a line of code above it).

Posted: Fri Dec 20, 2002 8:11 am
by BackupUser
Restored from previous forum. Originally posted by Froggerprogger.

Here are 5 samples of handling variables:

Code: Select all

;1st example: temp is not available to the procedure if not defined with 'Global'.;BUT you can use 'Shared' inside the procedure to get access to it.

temp.s = "hallo"
Procedure MyMsg()
    Shared temp.s
    
    temp = temp + "!"
    MessageRequester(temp,temp,0)
EndProcedure
MyMsg()
MessageRequester(temp,temp+" (check)",0)



;2nd example: Defines temp as Global, so it is available to all procedures

Global temp2.s
temp2.s="hallo2"
Procedure MyMsg2()
    temp2 = temp2 + "!"
    MessageRequester(temp2,temp2,0)
EndProcedure
MyMsg2()
MessageRequester(temp2,temp2+" (check)",0)



;3rd example: You can of course mix the 1st and the 2nd using Global AND Shared



;4th example: Defines a variable as global and uses inside a procedure the command protected to create
;a samenamed variable, but without the value of the global.

Global temp3.s
temp3.s="hallo3"
Procedure MyMsg3()
    Protected temp3.s
    temp3 = temp3 + "!!!! (protected)"
    MessageRequester(temp3,temp3,0)
EndProcedure
MyMsg3()
MessageRequester(temp3,temp3+" (check)",0)



;5th example: Uses the 'ProcedureReturn' to change the value of the variable in the main program

temp4.s = "hallo4"
Procedure.s MyMsg4(dummy.s)
    dummy = dummy + "!"
    MessageRequester(dummy, dummy,0)
    ProcedureReturn dummy
EndProcedure
temp4 = MyMsg4(temp4)
MessageRequester(temp4, temp4+" (check)",0)
Keep in mind that all non-global variables (the procedure's temp3 and the dummy) will NOT keep their value between 2 procedure-calls.
There is no command in PB like the 'STATIC' in QB (I suppose it was 'STATIC') to keep this value only for this procedure. (BASIC is not BASIC!)
For this you have to define it as 'Global' before the procedure-call.

Purebasic - what a nice name for a girl-friend

Posted: Mon Dec 23, 2002 12:53 am
by BackupUser
Restored from previous forum. Originally posted by JimmyClif.

PB, FroggerProgger,

Just wanted to say thanks for the little code sample. I'll work on it..

Cheers,
JC