ch. 5: functions

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:28 PM on 10/19/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

21 Terms

1
New cards

benefits of modularizing a program with functions

  • simpler code

  • code reuse

  • better testing

  • faster development

  • easier facilitation of teamwork


2
New cards

black box analogy

when you make your own functions, you will want to make them appear as black boxes to other programmers

  • there are fewer details that you need to keep in mind


3
New cards

implementing functions

a function to calculate the volume of a cube

  • what does it need to do its job?—-input

  • what does it answer with—-output

when writing this function

  • pick a name

  • declare a variable to receiver incoming argument

pull all info together to make the first line of the function’s definition

use return statement to get result


4
New cards

void functions

a function without a return statement

return None

5
New cards

testing a function

in order to test the function, your program should contain:

  • definition of the function

  • statements that call the function


6
New cards

docstrings

describe what your function does

7
New cards

main function

required function name used by other common languages

8
New cards

using functions: order

important that you define any function before you call it

but, a function can be called from within another function before the former has been defined

9
New cards

parameter variable

  • declared in the called function

  • initialized w/ the value of the argument value

  • used as a variable inside the called function


10
New cards

parameter passing

parameter variables receive the argument values supplied in the function call


the argument value may be:

  • a variable

  • a literal value


11
New cards

pass-by-value

the called function receives a copy of the argument’s value and works exclusively with that copy

  • for immutable objects, if changed inside a function, Python leaves the original object alone and forces the local parameter variable to point to a brand-new copy of the object

  • (int, float, str, tuple) can’t be change after made


12
New cards

pass-by-reference

Python copies the argument object’s reference-not the object itself- into the corresponding parameter

  • for mutable objects

    • objects like list, dict, and set can be changed in place

    • bc the function receives a copy of the reference pointing to the exact same object in memory, any in-place modifications will change the original object


13
New cards

return values

does two things:

  • immediately terminates the function

  • passes the return value back to the calling function


14
New cards

reusable functions

find repetitive code

  • may have different values but same logic


15
New cards

variable scope

can be declared:

  • inside a function

    • known as ‘local variables

    • only available inside this function

    • parameter variables of a function are local variables

  • outside of a function

    • ‘global scope’

    • can be used (and changed) by coed in any function


16
New cards

how do u chose between which variable scope

the scope of a variable is the part of the program in which it is visible and can be accessed

17
New cards

local variables of functions

variables declared inside one function are not visible to other functions

  • sideLength is local to main

  • Using it outside main will cause a compiler error


18
New cards

global variables

variables that are defined outside functions

a global variable is visible to all functions that are defined after it

any function that wishes to modify a global variable must include a global declaration

19
New cards

variable lifetime

the duration for which the variable exists in memory

  • global: created when they are defined in the main body of the script and persist until the entire program ends or the variable is explicitly deleted using keywords del

  • local: created when the function finishes execution or encounters a return statement


20
New cards

recursive functions

a function that calls itself

solves a problem by using the solution of the same problem with simpler inputs

to terminate, there must be special cases for the simplest inputs

21
New cards

string slicing

if s is a string, the expression s[start : stop : step] returns the portion of the string from inex start to index stop at a step size step