Chapter 5 Flashcards

(37 cards)

1
Q

What is a function?

A

A group of statements in a program that perform a specific task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is modularization in programming?

A

Breaking a program into functions so each task is in its own function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the benefits of using functions?

A

Simpler code, code reuse, easier testing/debugging, faster development, and easier teamwork

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a void function?

A

A function that executes its statements and then terminates without returning a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a value-returning function?

A

A function that executes statements and returns a value to the caller

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules for naming functions in Python?

A

Cannot be a keyword, no spaces, must start with a letter/underscore, can include letters/digits/underscores, case sensitive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a function definition?

A

Code that specifies what a function does, starting with a header and containing a block of statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a function header?

A

The first line of a function: starts with def, includes the function name, parentheses, and a colon

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the main function?

A

The function that defines the mainline logic of the program and calls other functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the pass keyword used for in functions?

A

To create an empty placeholder function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a local variable?

A

A variable assigned inside a function; it can only be accessed within that function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the scope of a local variable?

A

Limited to the function in which it is created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an argument?

A

A piece of data sent into a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a parameter?

A

A variable inside a function that receives the value of an argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are multiple arguments passed to functions?

A

By position; the first argument matches the first parameter, and so on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does “pass by value” mean in Python functions?

A

Changes made to parameters inside a function do not affect the original arguments

17
Q

What is a keyword argument?

A

An argument that specifies which parameter it corresponds to using parameter=value

18
Q

What are keyword-only parameters?

A

Parameters that must be passed using keyword arguments (declared after * in the parameter list)

19
Q

What are positional-only parameters?

A

Parameters that only accept positional arguments (declared before / in the parameter list)

20
Q

What is a default argument?

A

A parameter with a predefined value that is used if no argument is passed

21
Q

What is a global variable?

A

A variable declared outside all functions, accessible from anywhere in the program

22
Q

Why should global variables generally be avoided?

A

They make debugging harder, reduce portability, and make programs harder to understand

23
Q

What is a global constant?

A

A global name that references a value that should never change

24
Q

What is the difference between a void function and a value-returning function?

A

Void executes code and ends; value-returning executes code and sends a value back

25
What is the Python standard library?
A collection of prewritten functions provided with Python
26
How do you use functions from a module?
Write import module_name at the top, then call functions with module.function()
27
What functions are in the random module?
randint, randrange, random, and uniform
28
What does random.seed() do?
Sets the seed for random number generation
29
How do you write a value-returning function?
Use def and include a return expression statement
30
What is an IPO chart?
A chart that documents a function’s Input, Processing, and Output
31
What is a Boolean function?
A function that returns either True or False
32
Can Python functions return multiple values?
Yes, using a return statement with comma-separated values
33
What does returning None from a function mean?
It indicates “no value,” often used to signal an error
34
What functions are in the math module?
Examples include sqrt, sin, cos, tan, log, pi, and e
35
What is modularization with modules?
Grouping related functions into separate files for reuse and organization
36
What is a menu-driven program?
A program that displays a list of operations (menu) and allows the user to choose
37
What does if __name__ == '__main__': do?
Ensures the main function runs only when the program is executed directly, not when imported as a module