|
Post by QwertyCM on May 26, 2017 15:49:41 GMT -6
Hi there, I'm back with more questions xD. What is the return statement used for? I was looking over the script for the Dragon Realm thing with the 2 caves and I saw part of it use the statement return. def chooseCave():
cave = ''
while cave != '1' and cave != '2':
print('Which cave will you go into? (1 or 2)')
cave = input()
return cave My question is what exactly is it doing at this point. Any explanation would be very much appreciated .
|
|
|
Post by spohnz on May 26, 2017 16:13:45 GMT -6
Good! welcome back. In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after where the subroutine was called, known as its return address.
for instance, in your code here what is being returned is the input value. so it would be the string value '1' or '2' that the rest of the program needs to decide. I hope this explains it. If not then I can provide other examples to point out. But basically all it does is return a value back to the main program.
|
|
|
Post by QwertyCM on May 26, 2017 17:25:48 GMT -6
Hmm...I'm still not getting it sorry xD
|
|
|
Post by spohnz on May 26, 2017 18:03:12 GMT -6
No problem, I'll try to explain it in a different way. Lets say that you are tasked with writing a program that will get you several different quotes for a specific item online. lets also say that there are 15 different web sites you need to scrape quotes from and compile them in a list. well you may create a function for each web site that has the item listed. i.e. walmart, ebay, amazon, and so on. the item that you scraped will be what you want to return to the main script after the sub process (function) is complete.
#################################### def walmart(): wscrape=scrape('newToolBox') return wscrape def ebay(): escrape=scrape('newToolBox') return escrape def amazon(): ascrape=scrape('newToolBox') return ascrape #So when you run the main script each sub process above (after called) will return a different value to the main script which would be below this line.
walmart() print(wscrape) ebay() print(escrape) amazon() print(ascrape) ###############################################
So a function will not run. it is like a variable that is stored in memory until it is called on to run.
|
|