Unit 3 - Functions, Parameters And Arguments

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

35 Terms

1
New cards

What Is A Function?

Named block of code that performs a specific task

2
New cards

Decomposition

Break down complex problems into smaller, manageable pieces

3
New cards

Repetition Avoidance

Write code once and reuse it multiple times - this makes code shorter, easier to read, and less prone to errors

4
New cards

Readability & Reasoning

Simple, well-named functions make it easier to understand what a program does

5
New cards

Abstraction

Hide complex details and focus on the essential logic

6
New cards

How To Call Functions?

Done by using the function’s name followed by parentheses ()

7
New cards

Arguments

A value passed to a function when it’s called

8
New cards

Parameters

A variable inside the function definition that receives an argument’s value

9
New cards

Execute Functions

Function code runs

10
New cards

Return Value

The value a function sends back to the callerP

11
New cards

How Do Positional Arguments Work?

Matched according to position (order)

12
New cards

Risk Of Positional Arguments

Easy to get confused and send arguments in the wrong order

13
New cards

How Do Named Arguments (Keyword Arguments) Work?

Explicitly name the parameter

14
New cards

Benefit Of Named Arguments (Keyword Arguments)

Order can be arbitrary; improves readability

15
New cards

Optional Parameters (Default Values)

Parameters can have default values specified in the function

16
New cards

Optional Parameters (Default Values) Rule

Optional parameters must come after all mandatory parameters

17
New cards

What Is Scope?

Scope refers to the region of a program where a variable is recognised and can be accessed

18
New cards

Local Scope Definition

Variables defined inside a function are in the local scope

19
New cards

Local Scope Visibility

They are only visible and accessible within that specific function

20
New cards

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

21
New cards

Global Scope Definition

Variables defined outside of any function, at the top level of a script, are in the global scope

22
New cards

Global Scope Visibility

They are visible and accessible from anywhere in the program, including inside functions

23
New cards

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

24
New cards

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

25
New cards

Modifying Global Variables

To explicitly change a global variable from within a function, you must use the global keyword

26
New cards

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

27
New cards

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

28
New cards

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)

29
New cards

How Does A Base Case Work?

An if statement that handles the simplest possible input, returning a direct value without making another recursive call

30
New cards

Purpose of Recursive Step

Breaks the problem down into a simpler version of itself and calls the function again with modified parameters

31
New cards

How Does Recursive Step Work?

Usually involves calculations that move the input closer to the base case

32
New cards

Self-Call

A function invoking itself

33
New cards

Stack Overflow

Occurs if the base case is missing or never reached

34
New cards

Forgetting The Base Case

This is the most common error, leading to infinite recursion

35
New cards

Parameters Don’t Change

If the parameters in the recursive call don’t move closer to the base case, you’ll get infinite recursion