Even more Functions CS 1111 – SEPTEMBER 23, 2019
Local / Global varibles Local Variables Arguments and any variables declared inside a function Cannot be seen by other functions or code outside the function If they share a name with a variable outside the function, Python still treats them as though they were a separate variables Local variables disappear when the function ends Global variables Defined outside of the function Can be referenced in any function AFTER the variable is defined global keyword required to change a variable in a function.
Local Variables number = 0 def main(): number = int(input( 'Enter a number: ' )) show_number() def show_number(): print( 'The number you entered is ' , number) main()
Local Variables number = 0 def main(): number = int(input( 'Enter a number: ' )) show_number() def show_number(): print( 'The number you entered is ' , number) main() Local variable
Global Variables number = 0 def main(): global number number = int(input( 'Enter a number: ' )) show_number() def show_number(): print( 'The number you entered is ' , number) main()
Calling Functions with Named/Optional parameters Python only allows on function to exist with a given name No overloading However, it does support optional and named arguments Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); This defines default values for the variables name and school, making them optional
Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); #calling function with no arguments my_function() #calling function with one argument (based on order) my_function (“Will”) #calling function with named parameter my_function (school=“WVU”) #calling function with parameter passing (match number of parameters) my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);
Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); “Mary goes to UVA” #calling function with no arguments my_function() #calling function with one argument (based on order) my_function (“Will”) #calling function with named parameter my_function (school=“WVU”) #calling function with parameter passing (match number of parameters) my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);
Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); #calling function with no arguments my_function() “Will goes to UVA” #calling function with one argument (based on order) my_function (“Will”) #calling function with named parameter my_function (school=“WVU”) #calling function with parameter passing (match number of parameters) my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);
Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); #calling function with no arguments my_function() #calling function with one argument (based on order) “Mary goes to WVU” my_function (“Will”) #calling function with named parameter my_function (school=“WVU”) #calling function with parameter passing (match number of parameters) my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);
Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); #calling function with no arguments my_function() #calling function with one argument (based on order) my_function (“Will”) #calling function with named parameter my_function (school=“WVU”) “Will goes to WVU” #calling function with parameter passing (match number of parameters) my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);
Example: def my_function (name=“Mary”, school=“UVA”): print(name + “ goes to “ + school); #calling function with no arguments my_function() #calling function with one argument (based on order) my_function (“Will”) #calling function with named parameter my_function (school=“WVU”) #calling function with parameter passing (match number of parameters) “Will goes to WVU” my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);
Importing You can import your own code from another Python file Say our my_function () is in a file called “named_parameters.py” import named_parameters # this imports a file called # named_parameters.py # this executes the function called my_function in that file named_parameters.my_function (“Will”, “WVU”) Format: name_of_file.name_of_function()
In Class Activity http://www.cs.virginia.edu/~up3f/cs1111/inclass/activity- function.html
Recommend
More recommend