May 09, 2024, 11:17:39 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


PASSING STRING TO FUNCTION

Started by TexasPete, January 08, 2009, 03:22:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TexasPete

DECLARE CHANGE (A:STRING)
DEF PATH:STRING

SUB CHANGE (PATH)
''DO SOMETHING '


ENDSUB
RETURN

NEED A LITTLE HELP ON DECLARING AND PASSING A STING INTO A SUB, DOING SOMETHING TO IT AND THEN RETURNING THE CHANGED STRING BACK.
If any body has a second thanks
Texas Pete


LarryMc

compile as a "console" exe

DECLARE CHANGE (A:STRING)
DEF PATH:STRING

path =" This is what I start with "
print path

change(path)

print path
print
print "Press any key to close"

do:until inkey$<>""
end
SUB CHANGE (A:string)
a= a+" and I added this inside the sub"

RETURN

ENDSUB



Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

Would this also work?  Based on how EBasic works, this should work.

def a:string
a = "Testing
print a
change()
print a
print "Press any key to END"
do:until inkey$<>""
end

sub change()
a = a+"more text"
return
endsub
When all else fails, get a bigger hammer.

LarryMc

January 08, 2009, 09:38:18 PM #3 Last Edit: January 08, 2009, 09:40:16 PM by Larry McCaughn
Bill,
Your example will most defintely work.

However, I have found that, for me, my example would be the safer to use in large programs.
In those cases you can easily use a variable in a sub for something like a loop counter and find out it is a global variable used also in another sub and have them interfere with each other.

For that reason I always "AUTODEFINE "OFF" and pass variables to subs and define local variables inside a sub.
I run into less problems that way.

Another variation of our 2 examples that would preserve the original in case you need something like an "Are you sure? " test:
def a:string

declare change(in:string),string

a = "Testing "

print a,change(a)

print "Press any key to END"
do:until inkey$<>""
end

sub change(z:string)
z="more text"
return z
endsub


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library