Even more Functions CS 1111 SEPTEMBER 23, 2019 Local / Global - - PowerPoint PPT Presentation

even more
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Even more Functions

CS 1111 – SEPTEMBER 23, 2019

slide-2
SLIDE 2

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.

slide-3
SLIDE 3

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()

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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()

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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”);

slide-8
SLIDE 8

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”);

“Mary goes to UVA”

slide-9
SLIDE 9

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”);

“Will goes to UVA”

slide-10
SLIDE 10

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”);

“Mary goes to WVU”

slide-11
SLIDE 11

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”);

“Will goes to WVU”

slide-12
SLIDE 12

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”);

“Will goes to WVU”

slide-13
SLIDE 13

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()

slide-14
SLIDE 14

In Class Activity

 http://www.cs.virginia.edu/~up3f/cs1111/inclass/activity-

function.html