1/34
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What Is A Function?
Named block of code that performs a specific task
Decomposition
Break down complex problems into smaller, manageable pieces
Repetition Avoidance
Write code once and reuse it multiple times - this makes code shorter, easier to read, and less prone to errors
Readability & Reasoning
Simple, well-named functions make it easier to understand what a program does
Abstraction
Hide complex details and focus on the essential logic
How To Call Functions?
Done by using the function’s name followed by parentheses ()
Arguments
A value passed to a function when it’s called
Parameters
A variable inside the function definition that receives an argument’s value
Execute Functions
Function code runs
Return Value
The value a function sends back to the callerP
How Do Positional Arguments Work?
Matched according to position (order)
Risk Of Positional Arguments
Easy to get confused and send arguments in the wrong order
How Do Named Arguments (Keyword Arguments) Work?
Explicitly name the parameter
Benefit Of Named Arguments (Keyword Arguments)
Order can be arbitrary; improves readability
Optional Parameters (Default Values)
Parameters can have default values specified in the function
Optional Parameters (Default Values) Rule
Optional parameters must come after all mandatory parameters
What Is Scope?
Scope refers to the region of a program where a variable is recognised and can be accessed
Local Scope Definition
Variables defined inside a function are in the local scope
Local Scope Visibility
They are only visible and accessible within that specific function
Local Scope Lifetime
They are created when the function is called and destroyed when the function finishes executing (returns) - each function call creates a fresh set of local variables
Global Scope Definition
Variables defined outside of any function, at the top level of a script, are in the global scope
Global Scope Visibility
They are visible and accessible from anywhere in the program, including inside functions
Read-Onl Default Of Global Scope
By default, functions can read global variables, but they cannot directly modify them - attempting to assign a new value to a global variable inside a function actually creates a new local variable with the same name, shadowing the global one
Shadowing
When a local variable has the same name as a global variable, the local variable “hides” or “shadows” the global one within the function’s scope - the global variable becomes accessible again once the function exists
Modifying Global Variables
To explicitly change a global variable from within a function, you must use the global keyword
What Is Recursion?
Recursion is a programming technique where a function calls itself to solve a problem - it’s an alternative to using loops for repetition
How Recursion Works
A recursive function breaks a problem down into smaller, identical sub-problems - it solves the simplest version of the problem directly (the base case) and then uses the solution to the simpler sub-problems to build up the solution to the original problem
Purpose Of Base Case
A condition that stops the recursion - without a base case, the function would call itself infinitely (leading to a stack overflow error)
How Does A Base Case Work?
An if statement that handles the simplest possible input, returning a direct value without making another recursive call
Purpose of Recursive Step
Breaks the problem down into a simpler version of itself and calls the function again with modified parameters
How Does Recursive Step Work?
Usually involves calculations that move the input closer to the base case
Self-Call
A function invoking itself
Stack Overflow
Occurs if the base case is missing or never reached
Forgetting The Base Case
This is the most common error, leading to infinite recursion
Parameters Don’t Change
If the parameters in the recursive call don’t move closer to the base case, you’ll get infinite recursion