W3T2 - Functions
W3T2 - Functions
Learning Goals
Example
Code:
Basic Structure of a Function
Sample Function:
Output:
Summary of Functions
Reusability: Functions allow you to reuse code, which makes programs shorter and more readable.
Modularity: Functions help break down complex problems into smaller, more manageable pieces.
Abstraction: When you use a function, you don't need to know how it works internally—just what it
does and how to use it.
Parameters: Functions can take parameters (input values) that allow you to pass data into them.
Return Values: Functions can return values that can be used in other parts of your program.
Global Variable
Global Variable:
A global variable is a variable that is defined outside of any function and is accessible
throughout the entire program, including inside functions (unless shadowed by a local
variable with the same name). The scope of a global variable is the entire module or
script.
• Scope: Global variables are accessible from any part of the program, including inside
functions (if not shadowed).
• Lifetime: Global variables exist for the duration of the program's execution. They are
created when the program starts and destroyed when the program ends.
• Modification: To modify a global variable inside a function, you must explicitly declare
it as global using the global keyword.
Global Variable Example
•The first print statement inside the function prints global_var's value, which is 20.
•The second print statement outside the function also prints the same value of global_var,
which remains 20.
•This illustrates that the global variable global_var is accessible both inside and outside the
function, and its value remains consistent across both contexts since no modifications are made
to it.
Local Variable
Local Variables:
A local variable is a variable that is defined inside a function and is only accessible
within that function. It cannot be accessed from outside the function in which it is
declared. The scope of a local variable is limited to the function block where it is
defined.
•This code snippet illustrates how a variable defined within a function is local to that function,
meaning it can only be accessed or modified inside that function.
•Local variables are useful for temporary storage of data that only needs to be used within a
specific function.
Summary of Differences
my_function()
print("Outside function, global_var:",
global_var) # This prints the original global
variable
Activity – Modify to include
functions
•The program takes the radius as input and calculates the area
using the formula πr².
•The area is then printed.
Answer - Modify to include
functions
With Functions: The code is broken into modular parts, where each
function handles a specific task. This improves readability,
maintainability, and reusability. Functions allow the same logic to be
reused in different parts of the program or even in different programs.
RECALL QUESTIONS
What scope of variable do
we use INSIDE a function?
What is a function?
Why do we use Functions?
What algorithm construct
aligns with a Function?
What is the Key Word in
python to declare a
function?
Extra Resources
Functions:
https://www.w3schools.com/python/python_functions.asp