Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Swap word function
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ssikmace




PostPosted: Mon Apr 05, 2004 6:55 pm   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
omni




PostPosted: Mon Apr 05, 2004 7:23 pm   Post subject: (No subject)

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




PostPosted: Mon Apr 05, 2004 7:40 pm   Post subject: (No subject)

Thats what i tried but i couldn't get it to work.
ssikmace




PostPosted: Mon Apr 05, 2004 7:47 pm   Post subject: (No subject)

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




PostPosted: Mon Apr 05, 2004 7:58 pm   Post subject: (No subject)

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




PostPosted: Mon Apr 05, 2004 8:03 pm   Post subject: (No subject)

code:

result word1 + " " + word2

? Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ssikmace




PostPosted: Mon Apr 05, 2004 8:11 pm   Post subject: (No subject)

lol good enough for me, thx. As long as my variables remain switched.
Tony




PostPosted: Mon Apr 05, 2004 8:29 pm   Post subject: (No subject)

well if you're happy with that, why not just use
code:

function switch(word1,word2:string) :string
return word2 + " " + word1
end switch

Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: