0% found this document useful (0 votes)
9 views2 pages

Python Functions & Variable Scope Guide

The document outlines a lecture on Functions and Variable Scope in Python for a Computer Science course. It explains the definition and usage of functions, variable scope, and provides examples of local and global variables. Additionally, it covers advanced topics such as default arguments, recursion, and lambda functions, along with a homework assignment to write a recursive Fibonacci function.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Python Functions & Variable Scope Guide

The document outlines a lecture on Functions and Variable Scope in Python for a Computer Science course. It explains the definition and usage of functions, variable scope, and provides examples of local and global variables. Additionally, it covers advanced topics such as default arguments, recursion, and lambda functions, along with a homework assignment to write a recursive Fibonacci function.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

💻 3.

Computer Science 110: Intro to Programming

Date: Nov 4, 2025


Professor: Prof. T. Huang

Topic: Functions and Variable Scope in Python

Concept Overview:
Functions are reusable blocks of code designed to perform a specific task. They
improve modularity, readability, and debugging efficiency.

Function Basics:

Defined with def keyword.

May include parameters (inputs) and a return value (output).

Can be called multiple times throughout a program.

Example:

def greet(name):
print("Hello,", name)

greet("Alice")

Output: Hello, Alice

Variable Scope:

Local variables: exist only inside the function.

Global variables: exist throughout the program.

Scope hierarchy (LEGB): Local → Enclosing → Global → Built-in.

Example of Scope Issue:

x = 10
def change():
x = 5
print(x) # local
change()
print(x) # global

Output:

5
10

Advanced Topics:

Default arguments and keyword arguments.

Recursion (functions calling themselves).


Lambda functions for short, throwaway logic.

Professor’s Tip:

“If you find yourself copying code more than twice—write a function instead.”

Homework:

Write a recursive function that returns the Fibonacci sequence up to n.

Bonus: Add type hints and docstrings.

You might also like