Introduction to Programming Concepts with Functions and Variables

 
Course A201:
Introduction to Programming
 
12/9/2010
 
Review – Part 2
 
Functions
Global variables vs local variables
 
Recall Function
 
 [functions]
I am a function.
I will perform a
certain job.
 
input1, input2, input3, …
 
output1, output2, output3, …
 
    You give
 me
some inputs
 
    I give you some
outputs back,
explicitly
 or
implicitly
 
Functions: define and call
 
def
 
circle_area
(radius):
     
 return 
(3.14159 * radius ** 2)
 
 
 
  circle_area(5)
  print(
“Now I know the area of the circle”
)
 
 
    This
 is the definition of a
function. This block define
what job will 
circle_area
perform, input and output.
 
This
 is how you call the function: 
name
 and 
parentheses
 
Functions: define and call
 
def
 
instructions
():
 
print(
“““Welcome to the greatest intellectual
challenge of all time: Tic-Tac-Toe. This will be a
showdown between your human brain and my
silicon processor.”””
)
 
instructions()
 
This
 is how you call the function: 
name
 and 
parentheses
 
    This
 is the definition.
 
Functions: Return values
 
So, from the above two examples:
Function 
circle_area() 
does return a value, which
is the area of a circle
Function 
instructions() 
does NOT return a value,
and by default the return value will be 
None
 
The keyword 
return
 
Functions: Return values
 
You have two functions:
 
def
 
circle_area1
(radius):
 
area = 3.14159 * radius ** 2
 
return 
area
 
def
 
circle_area2
(radius):
 
area = 3.14159 * radius ** 2
 
print(area)
 
 
Functions: Return values
 
1) What’s the output of
 
print(circle_area1(5))
 
 
2) And, what’s the output of
 
print(circle_area2(5)))
 
Functions: Return values
 
1) What’s the output of
 
print(circle_area1(5))
 
 
2) And, what’s the output of
 
print(circle_area2(5)))
 
When a function doesn’t have a return statement in its
definition, by default the return value will be 
None
78.53975
78.53975
None
 
Functions: Return values
 
Be careful with the requirement, if it tells you to
write a function which 
return
 the value, then you
need to write a return statement
 
It is always better to store the return value in a
variable if the function has the return statement.
 
area = circle_area1(5)
 
print(area)
This is called: Catch the return value!
 
Parameters and Arguments
 
def
 
circle_area
(radius):
     
 return 
(3.14159 * radius ** 2)
 
 
  circle_area(5)
  print(
“Now I know the area of the circle”
)
 
 
Parameters
 
Arguments
 
Parameters and Arguments
 
def
 
add
(a, b):
     
 return 
(a+b)
 
  add(2, 4)
 
 
Parameters
 
Arguments
 
Parameters and Arguments
 
Parameters are essentially variable names
inside the parentheses of a function header:
add(a,b), a and b are parameters of the function
add
You can have 0, 1 or many parameters
 
The function receives values/arguments
through its parameters e.g. add(1,2)
The code in the function 
add now treats a and b
as variables with values 1 and 2
 
Parameters and Arguments
 
Example from the final practice:
 
def newYearGreeting(name= "None", year= 2010):
 
print("Happy New Year,", name, "!", "It is", year,
"!\n")
 
Parameters and Arguments
 
Example from the final practice:
 
Parameters and Arguments
 
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 header is perfectly fine:
def monkey_around(bananas = 100, barrel_of = "yes"):
But this isn't:
def monkey_around(bananas = 100, barrel_of):
The above header will generate an 
ERROR
.
 
Global variables
 
Global variables: variables that are initialized
in the main script
Local variables: variables that are initialized in
the definition of a function
Global variables
def
 
my_function
():
 
value = 1
 
print(
“In the local scope, value is”
, value)
value = 10
print(
“In the global scope, value is”
, value)
Two ‘value’s have the same names, but they don’t
refer to the same variable
 
Local variable
 
Global variable
 
Global variables
 
If you have a family friend named Bill Clinton. While
in your neighborhood,  ‘Bill Clinton’ refers to this
friend by default. But out of your neighborhood, ‘Bill
Clinton’ refers to the formal president by default.
 
Global variables
def
 
my_function1
():
 
value = 1
 
print(
“In my function 1, value is”
, value)
def
 
my_function2
():
 
print(
“In my function 2, value is”
, value)
value = 10
print(
“In the global scope, value is”
, value)
 
    Local variable or
global variable?
Global variables
def
 
my_function1
():
 
value = 1
 
print(
“In my function 1, value is”
, value)
def
 
my_function2
():
 
print(
“In my function 2, value is”
, value)
value = 10
print(
“In the global scope, value is”
, value)
 
 
Thi
s is a global
variable
.
 
Global variables
 
So, if there is an assignment statement, such as
value = 1, in the definition of your local function, this
value will be automatically considered as you create
a new variable in your function, with the same name,
but refers to a different value. This variable has
nothing to do with the global one.
But if there is no assignment statement in your local
function, as the above example, it will be considered
as you are referring to the global variable
The ‘nearest’ policy
 
 
Global variables
 
Then, how to change a global variable in your local
function?
Use the keyword: 
global
 
Compare two examples
 
 
Global variables
 
def
 
my_function
():
 
value = 1
 
value = 10
print(
“At first, value is”
, value)
my_function()
print(
“Now, value is”
, value)
 
Output:
At first, value is 10
Now, value is 10
Global variables
def
 
my_function
():
 
global
 value
 
value = 1
value = 10
print(
“At first, value is”
, value)
my_function()
print(
“Now, value is”
, value)
Output:
At first, value is 10
Now, value is 1
 
 
You explicitly declare
this is a global variable
Global variables
To sum up:
Situation 1:
def
 
my_function
():
 
value = 1
 
print(
“In my function 1, value is”
, value)
value = 10
my_function()
print(
“In the global scope, value is”
, value)
 
 
Thi
s is a local
variable
.
 
 
They
 are different.
Global variables
To sum up:
Situation 2:
def
 
my_function
():
 
print(
“In my function 1, value is”
, value)
value = 10
my_function()
print(
“In the global scope, value is”
, value)
 
 
Thi
s is a global
variable
.
 
 
Both
 of them refer to the same
variable.
Global variables
To sum up:
Situation 3:
def
 
my_function
():
 
global
 value
 
value = 1
 
print(
“In my function 1, value is”
, value)
value = 10
my_function1()
print(
“In the global scope, value is”
, value)
 
 
Global ‘value’ is
changed to 1.
 
 
They now refer to
 the same
variable, because you used the key
word global.
Slide Note
Embed
Share

Dive into the fundamental aspects of programming with the concepts of functions, global variables, and local variables. Understand how to define, call, and return values from functions, while distinguishing between different types of variables.

  • Programming Concepts
  • Functions
  • Variables
  • Global vs Local
  • Return Values

Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Course A201: Introduction to Programming 12/9/2010

  2. Review Part 2 Functions Global variables vs local variables

  3. Recall Function [functions] input1, input2, input3, You give me some inputs I am a function. I will perform a certain job. I give you some outputs back, explicitly or implicitly output1, output2, output3,

  4. Functions: define and call def circle_area(radius): return (3.14159 * radius ** 2) This is the definition of a function. This block define what job will circle_area perform, input and output. circle_area(5) print( Now I know the area of the circle ) This is how you call the function: name and parentheses

  5. Functions: define and call def instructions(): print( Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe. This will be a showdown between your human brain and my silicon processor. ) This is the definition. instructions() This is how you call the function: name and parentheses

  6. Functions: Return values So, from the above two examples: Function circle_area() does return a value, which is the area of a circle Function instructions() does NOT return a value, and by default the return value will be None The keyword return

  7. Functions: Return values You have two functions: def circle_area1(radius): area = 3.14159 * radius ** 2 return area def circle_area2(radius): area = 3.14159 * radius ** 2 print(area)

  8. Functions: Return values 1) What s the output of print(circle_area1(5)) 2) And, what s the output of print(circle_area2(5)))

  9. Functions: Return values 1) What s the output of print(circle_area1(5)) 78.53975 2) And, what s the output of print(circle_area2(5))) 78.53975 None When a function doesn t have a return statement in its definition, by default the return value will be None

  10. Functions: Return values Be careful with the requirement, if it tells you to write a function which return the value, then you need to write a return statement It is always better to store the return value in a variable if the function has the return statement. area = circle_area1(5) print(area) This is called: Catch the return value!

  11. Parameters and Arguments Parameters def circle_area(radius): return (3.14159 * radius ** 2) Arguments circle_area(5) print( Now I know the area of the circle )

  12. Parameters and Arguments Parameters def add(a, b): return (a+b) add(2, 4) Arguments

  13. Parameters and Arguments Parameters are essentially variable names inside the parentheses of a function header: add(a,b), a and b are parameters of the function add You can have 0, 1 or many parameters The function receives values/arguments through its parameters e.g. add(1,2) The code in the function add now treats a and b as variables with values 1 and 2

  14. Parameters and Arguments Example from the final practice: def newYearGreeting(name= "None", year= 2010): print("Happy New Year,", name, "!", "It is", year, "!\n")

  15. Parameters and Arguments Example from the final practice:

  16. Parameters and Arguments 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 header is perfectly fine: def monkey_around(bananas = 100, barrel_of = "yes"): But this isn't: def monkey_around(bananas = 100, barrel_of): The above header will generate an ERROR.

  17. Global variables Global variables: variables that are initialized in the main script Local variables: variables that are initialized in the definition of a function

  18. Global variables def my_function(): value = 1 print( In the local scope, value is , value) Local variable Global variable value = 10 print( In the global scope, value is , value) Two value s have the same names, but they don t refer to the same variable

  19. Global variables If you have a family friend named Bill Clinton. While in your neighborhood, Bill Clinton refers to this friend by default. But out of your neighborhood, Bill Clinton refers to the formal president by default.

  20. Global variables def my_function1(): value = 1 print( In my function 1, value is , value) Local variable or global variable? def my_function2(): print( In my function 2, value is , value) value = 10 print( In the global scope, value is , value)

  21. Global variables def my_function1(): value = 1 print( In my function 1, value is , value) This is a global variable. def my_function2(): print( In my function 2, value is , value) value = 10 print( In the global scope, value is , value)

  22. Global variables So, if there is an assignment statement, such as value = 1, in the definition of your local function, this value will be automatically considered as you create a new variable in your function, with the same name, but refers to a different value. This variable has nothing to do with the global one. But if there is no assignment statement in your local function, as the above example, it will be considered as you are referring to the global variable The nearest policy

  23. Global variables Then, how to change a global variable in your local function? Use the keyword: global Compare two examples

  24. Global variables def my_function(): value = 1 value = 10 print( At first, value is , value) my_function() print( Now, value is , value) Output: At first, value is 10 Now, value is 10

  25. Global variables def my_function(): global value value = 1 You explicitly declare this is a global variable value = 10 print( At first, value is , value) my_function() print( Now, value is , value) Output: At first, value is 10 Now, value is 1

  26. Global variables To sum up: Situation 1: def my_function(): value = 1 print( In my function 1, value is , value) This is a local variable. They are different. value = 10 my_function() print( In the global scope, value is , value)

  27. Global variables To sum up: Situation 2: def my_function(): print( In my function 1, value is , value) This is a global variable. Both of them refer to the same variable. value = 10 my_function() print( In the global scope, value is , value)

  28. Global variables To sum up: Situation 3: def my_function(): global value value = 1 print( In my function 1, value is , value) They now refer to the same variable, because you used the key word global. Global value is changed to 1. value = 10 my_function1() print( In the global scope, value is , value)

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#