even more
play

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


  1. Even more Functions CS 1111 – SEPTEMBER 23, 2019

  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.

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

  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

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

  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

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

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

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

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

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

  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) “Will goes to WVU” my_function (“Will”, “WVU”) #calling a function with both named parameters in any order my_function (school=“WVU”, name=“Will”);

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

  14. In Class Activity  http://www.cs.virginia.edu/~up3f/cs1111/inclass/activity- function.html

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend