Even more Functions
CS 1111 – SEPTEMBER 23, 2019
Even more Functions CS 1111 SEPTEMBER 23, 2019 Local / Global - - PowerPoint PPT Presentation
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
CS 1111 – SEPTEMBER 23, 2019
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.
number = 0 def main(): number = int(input('Enter a number: ')) show_number() def show_number(): print('The number you entered is ', number) main()
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
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()
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
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”);
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”);
“Mary goes to UVA”
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”);
“Will goes to UVA”
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”);
“Mary goes to WVU”
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”);
“Will goes to WVU”
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”);
“Will goes to WVU”
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()
http://www.cs.virginia.edu/~up3f/cs1111/inclass/activity-
function.html