Learn Python: Functions
Which variables can be called in the blank spot in this code: counter = 0 def update(): new_counter = counter + 1 return new_counter _______
Just counter counter is global in scope, whereas new_counter only exists inside of updates.
What's an Argument?
Parameters in python are variables — placeholders for the actual values the function needs. When the function is called, these values are passed in as arguments.
What's calling a function?
Python uses simple syntax to use, invoke, or call a preexisting function. A function can be called by writing the name of it, followed by parentheses.
Given the following function, what will produce the output "There is no greater agony than bearing an untold story inside you."? def quote(x): print("There is no greater agony than bearing " + x + " inside you.")
quote("an untold story"
What line of code can be used to return a variable inner_var from a function back to the piece of code that called the function?
return inner_var return passes the variable back to the function caller/
How do you call a function called setup with no arguments?
setup()
How do you call update with a new_value of 20? def update(new_value = 10): old_value = new_value
update(20) Now, inside of update, new_value will be equal to 20.
What's a Return?
A return keyword is used to return a value from a Python function. The value returned from a function can be assigned to a variable which can then be used in the program.
What's a Function?
Rather than rewrite the same code in multiple places, a function may be defined using the def keyword. Function definitions may include parameters, providing data input to the function. Functions may return a value using the return keyword followed by the value to return.
What's a Multiple Parameter?
To define a function with multiple parameters, parameter names are placed one after another, separated by commas, within the parentheses of the function definition.
What happens when you call report()? time = "3pm" mood = "good" def report(): print("The current time is " + time) print("The mood is " + mood) print("Beginning of report") report()
Two Strings are printed: "The current time is 3pm" and "The mood is good." These two print statements are inside the report() function.
What line of code will call force with a value of 10 for mass and a value of 9.81 for acceleration? def force(mass, acceleration): force_val = mass*acceleration return force_val
force(10, 9.81) 10 is assigned to mass and 9.81 is assigned to acceleration.