Docsity

Kingston University CI4100: Python Function Study Guide, Lecture notes of Computer Science

Toggle sidebar
bg1
Topic 1: Functions (Python)
Definitions:
def: Keyword used to declare a new function block.
arguments: Values passed into a function as inputs.
return: Sends a calculated result value back out of the function block.
Scope: Variables inside a function cannot be seen from the outside.
Function: Section of a program that defines how to perform a specific task.
Example:
def calculate_tax(price, rate=0.20): # Arguments (with 1 default)
tax_amount = price * rate # Local Scope variable
return tax_amount # Output result
final_tax = calculate_tax(100) # Calls function
Code Breakdown:
rate=0.20: A default argument. If you don't supply a rate value when calling it,
the function automatically falls back to 0.20.
return tax_amount: Exits the function execution sequence and passes the
math outcome back up to the final_tax storage variable.
What to use and what to avoid:
-Avoid (Hardcoded Dictionary Lookups):
rate = 0.20
def tax(price):
return price * rate # Relies heavily on outside variables
-Use (Self-Contained Pure Functions):
def tax(price, rate):
return price * rate # Pass inputs directly into arguments
pf2

Partial preview of the text

Download Kingston University CI4100: Python Function Study Guide and more Lecture notes Computer Science in PDF only on Docsity!

Topic 1: Functions (Python)

Definitions:

 def: Keyword used to declare a new function block.

 arguments: Values passed into a function as inputs.

 return: Sends a calculated result value back out of the function block.

 Scope: Variables inside a function cannot be seen from the outside.

 Function: Section of a program that defines how to perform a specific task.

Example:

def calculate_tax(price, rate=0.20): # Arguments (with 1 default) tax_amount = price * rate # Local Scope variable return tax_amount # Output result final_tax = calculate_tax( 100 ) # Calls function

Code Breakdown:

 rate=0.20: A default argument. If you don't supply a rate value when calling it,

the function automatically falls back to 0.20.

 return tax_amount: Exits the function execution sequence and passes the

math outcome back up to the final_tax storage variable.

What to use and what to avoid:

- Avoid (Hardcoded Dictionary Lookups):

rate = 0. def tax(price): return price * rate # Relies heavily on outside variables

- Use (Self-Contained Pure Functions): def tax(price, rate): return price * rate # Pass inputs directly into arguments

Understand what the error tells us - (remember errors are our friends): Error Message Typical Cause Instant Fix NameError: name 'tax_amount' is not defined Trying to use a local variable outside its parent function block. Either print it inside the function or return the value to a global variable. TypeError: missing required positional argument Calling a function without passing the required number of inputs. Provide all missing values inside the parentheses when running the function.