Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Unsubscribed User
7,260 PointsArguments in function
Hi all!
I've finalized the Python Basics entirely just now. I've done it completely twice and understand, I believe, all concepts except one: arguments.
I find it somehow difficult to understand. I do understand that you have functions and you can give it arguments however I lack to see which impact or what they are for exactly.
To be more clear, I'll try to use the example of the letter game below:
def draw(bad_guesses, good_guesses, secret_word):
clear()
print("Strikes: {}/{}".format(len(bad_guesses), (len(secret_word) + 2)))
print("")
for letter in bad_guesses:
print(letter, end="")
print("\n\n")
for letter in secret_word:
if letter in good_guesses:
print(letter, end='')
else:
print("_", end='')
def play(done):
Whether we quote the 3 arguments in the draw function or the one in the play function or not, the programme seems to run just fine (I tried it).
So I do not understand why we mention them there. Is this for reference only? To know which variables are used inside the function? I've tried to search on the web for an explanation that I can take in, but unfortunately I haven't found one yet.
Many thanks in advance for your help !
1 Answer
Carlos Alberto Del Cueto Carrejo
13,817 PointsIn this case your arguments can make the function work, if you quote them you make them strings and that does not affect this specific functions. Let me set an example , lets say that you create a script and it helps you to monitor another computer trough a ping, which is something common in python. Its arguments can be an IP address and the the number of seconds you want the ping test to last. This allows you to use the same script for a lot of different IPs, and they are further away increase the time it takes them to respond the ping. In other words they help you to maintain reusability ;)
Eli Goodwin
29,502 PointsEli Goodwin
29,502 PointsYou probably need to post the rest of the program you wrote. Arguments are necessary, because as you start writing complex programs scope will come heavily into focus. Scope refers to where a variable is located in a program and its accessibility. There is local scope meaning a variable exists within a function and dies there. Nothing but the function which that variable is defined in can access it, which is VERY useful. And there is global scope where anything has access to that variable without it being passed as an argument.
Example: what will the output be of the following?
So what is happening here? Why is this function able to work without passing it arguments? Scope! The variables response are defined with global scope and also with local scope. Both are completely separate variables even though they have the same identifier. What happens when myFunction is modified to def myFunction(response)? It will now be able to access the global variable and change it. When you define a function in Python and it will search for local variables first, if it cannot find them locally it will go to the next level, and so on until it reaches global scope. If it cannot find the variable globally you will get an error message.
Your program works most likely because all of the variables were defined with global scope and don't need to be passed as arguments in this instance. If those variables were not defined globally the function would break every time you called it. It is an excellent habit to encapsulate your programs into functions and to avoid the use of global variables outside of those that are used as constants.