
-----------------------------------
ssikmace
Mon Apr 05, 2004 6:55 pm

Swap word function
-----------------------------------
I need to create a function that will swap the values for two string variables given as the paramaters for the function. It doesnt seem to hard but i just can't get one to work.

-----------------------------------
omni
Mon Apr 05, 2004 7:23 pm


-----------------------------------
wouldn't you create a temporary variable in the procedure to hold one of the values?

temp:=value1
value1:=value2
value2:=temp

something like that?

-----------------------------------
ssikmace
Mon Apr 05, 2004 7:40 pm


-----------------------------------
Thats what i tried but i couldn't get it to work.

-----------------------------------
ssikmace
Mon Apr 05, 2004 7:47 pm


-----------------------------------
This is what i tried but it argued that the "Left side of Assignment is not a variable"

function swap(word1,word2:string):string
var temp:string
temp:=word1
word1:=word2
word2:=temp
end swap

-----------------------------------
ssikmace
Mon Apr 05, 2004 7:58 pm


-----------------------------------
Ok ive fixed the previous problem so far but now how do i get it to have the switched words as my result, because its giving me "Function failed to give a result"

function swap (var word1, word2 : string) : string
    var temp : string
    temp := word1

    word1 := word2
    word2 := temp
end swap

var word1 : string
var word2 : string
get word1
get word2
put swap (word1, word2)

-----------------------------------
Tony
Mon Apr 05, 2004 8:03 pm


-----------------------------------

result word1 + " " + word2

? :lol:

-----------------------------------
ssikmace
Mon Apr 05, 2004 8:11 pm


-----------------------------------
lol good enough for me, thx. As long as my variables remain switched.

-----------------------------------
Tony
Mon Apr 05, 2004 8:29 pm


-----------------------------------
well if you're happy with that, why not just use

function switch(word1,word2:string) :string
return word2 + " " + word1
end switch

 :lol:
