Download presentation
1
Positional Parameters and Global Variables
Chapter 6 Part 2 Positional Parameters and Global Variables
2
Positional Parameters vs. Default Parameters
Positional Parameters and positional arguments mean that parameters get their values based solely on the position of the values sent. Ex. def birthday1(name, age) Name and age are positional parameters Ex. birthday1(“Jackson”, 1) “Jackson” and 1 are positional arguments The first parameter gets the first value sent, the second parameter gets the second value sent. If you switch the arguments, birthday1(1, “Jackson”) then name gets 1 and age gets Jackson, that is unless you specify.
3
Keyword Arguments Keyword arguments let you assign certain values to specific parameters regardless of order. If you say: birthday1(name = “Jackson”, age = 1) Or Birthday1(age = 1, name = “Jackson”) Name gets Jackson and age gets 1 in both scenarios. Keyword arguments let you pass values in any order. But their biggest benefit is clarity. Using them gives you a better idea of what the values represent.
4
Default Parameter Values
You have the option to assign default values to your parameters – values that get assigned to the parameters if no value is passed to them. Ex. def birthday2(name = “Jackson”, age = 1): This means if no value is given for name, it gets Jackson, and if no value is given for age it gets 1. So birthday2( ) won’t generate an error.
5
Default Values You can override default values: birthday2(name = “Katherine”) name is overridden birthday2(age = 12) age is overridden birthday2(name = “Katherine”, age = 12) both are overridden birthday2(“Katherine”, 12) gets the same results as previous. *Note: Once you assign a default value to a parameter in the list, you have to assign default values to all the parameters listed after it. So this function is fine: def food(fave = “pizza”, second = “spaghetti”, third = “chocolate”) But this one is not: def food(fave = “pizza”, second, third)
6
Global Variables and Constants
Through encapsulation, functions are totally sealed off and independent from each other and the main part of the program. The only way to get information to them is through their parameters, and the only way to get information out of them is through their return values. That is unless you use global variables Scopes – different areas of your program that are separate from each other.
7
Scopes variable 1 and 2 are local Variable 0 is global def func1()
variiable 0 variable 1 and 2 are local Variable 0 is global You could have two variables In function 1 and function 2 with the same name and they wouldn’t affect one another. It is different for global variables and local variables that have the same name def func1() variable1 def func2() variable 2
8
Global Reach Program -If you give a variable inside a function the same name as a global variable, you shadow the global variable. This is, you hide it with your new variable. When you change its value, it only changes the value of the local variable. *It is not a good idea to shadow a global variable. It can cause confusion when you think you are using a global value but you are not. - If you want to gain complete access to a global variable, us the keyword “global”, then the function has complete access to the value, so if it is changed in the local scope, it will also be changed in the global scope. Ex. Change_global()
9
Preparing for the Ch. 6 Test
Write a function that takes 3 integer parameters and returns the smallest of the 3. Write a function that takes 3 integer values (the coefficients of a quadratic equation) and returns the positive value of the quadratic formula (don’t worry about imaginary solutions). Write a function that takes one integer parameter. It should return true if the integer is positive and false if it is not positive. Write a function that takes 3 strings and returns one string which is the 3 strings put together with a ? between each word. Example: If I run the function with the strings “This” “is” “test” the function would return “This?is?test” (without the “ marks). Write a function that takes 2 integer parameters, one is hour the other is minute. Have your function return a string that is the time. The time will be in military time when you get it so hours of 13 is 1 pm etc. Also for midnight and noon have your function return the words midnight or noon. Call your function time. (10 points) Example: time(3,17) returns the string 3:17 am. Example: time(13,2) returns 1:02 pm Example time(0,0) should return midnight Example time(12,0) should return noon
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.