I made my own function to experiment with why we use
return, but am getting an error when I try to write it a different way.
I suppose I should also provide a little bit of background context; let’s say that I wanted to make a site that finds books people want to read. The user would type in the book’s title and that author, and while the website is loading, it will take the user to a page that says it’s looking for the book.
Below is a codebyte with the first way I wrote my code, which works like how I expected it to; the output says it is looking for a book by its specific author. (The example in the code is
Looking for "Divergent" by Veronica Roth... )
HOWEVER, I also wrote my code a second way — without using return — and it runs perfectly fine. So I tried to find out how
return was important to my first example…
…by using the variables outside of my function in this third example. Which obviously didn’t work, because (according to my understanding, anyway) we cannot treat parameters as variables outside of the function. That made sense, but the thing is, I can’t seem to figure out a way to make it work. So my question is, how can I use these variables outside my function to make them work? And well… the bigger question, really… how is using return important to this specific example? (As we’ve seen, the output is the same for both the above ways…)
OKAY I admit this was one long post. But now understanding this concept (and my own example LOL ) has really intrigued me, and I just can’t rest… what I’m saying is, THANK YOU, reader, for reading all of this, and if you take the time to answer my questions and attempt to make me understand… well, I have a million more thank you’s.