Python 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/97

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:21 PM on 3/30/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

98 Terms

1
New cards

What is a function?

A group of statements within a program that perform a specific task.

2
New cards

What is a modularized program?

A program where each task is in its own function.

3
New cards

What are the benefits of using functions?

1. Simpler code

2. Code reuse

3. Netter testing and debugging

4. Faster development

5. Easier facilitation of teamwork.

4
New cards

What is a void function?

A function that simply executes the statements it contains and then terminates.

5
New cards

What is a value-returning function?

A function that executes the statements it contains and then returns a value back to the statement that called it.

6
New cards

What are some examples of value-returning functions?

Input, int, and float functions.

7
New cards

What are the rules for naming a function?

1. Cannot use keywords

2. Cannot contain spaces

3. First character must be a letter or underscore

4. All other characters must be a letter, number, or underscore

5. Case-sensitive; uppercase and lowercase characters are distinct

8
New cards

What is the naming convention for functions?

Descriptive of the task carried out by the function, often includes a verb.

9
New cards

What does a function definition specify?

The functions name and a parameter list, the block of statements excuted when the function is called.

10
New cards

What is a function header?

The first line of a function, includes the keyword def, function name, parentheses, and colon.

11
New cards

What is a block?

A set of statements that belong together as a group, such as the statements included in a function.

12
New cards

How do you call a function?

By using its name followed by parentheses, including any arguments in parentheses that are required to be passed to the parameters of the function.

13
New cards

What happens when a function is called?

The interpreter jumps to the function and executes the statements in the block, then jumps back to the part of the program that called the function.

14
New cards

What is the main function?

Function called when the program starts

15
New cards

What does the main function do?

Defines the mainline logic of the program

16
New cards

What is the purpose of indentation in Python?

Each block must be indented, lines in block must begin with the same number of spaces or tabs. Do not mix spaces and tabs, use one or the other consistently throughout your source code.

17
New cards

What happens if you use both tabs and spaces for indentation in Python?

It can confuse the Python interpreter

18
New cards

What does IDLE do with the lines in a block?

Automatically indents them

19
New cards

How is a function call represented in a flowchart?

As a rectangle with vertical bars at each side and the name of the function displayed within the vertical bars.

20
New cards

Where is the function name written in a flowchart?

Inside the vertical bars of the rectangle symbol

21
New cards

Should you draw a separate flowchart for each function in a program?

Yes

22
New cards

What does the end terminal symbol in a flowchart usually read?

Return

23
New cards

What is another name for a hierarchy chart?

Structure chart

24
New cards

What does a box in a hierarchy chart represent?

A function in the program

25
New cards

What do the lines connecting boxes in a hierarchy chart illustrate?

The relationship of functions on the hierarchy chart.

26
New cards

Does a hierarchy chart show the steps taken inside a function?

No, it only shows the relationship between functions.

27
New cards

What function can be used to make a program wait for user input?

input function

28
New cards

What is a local variable?

A variable assigned a value inside a function and only available to that function.

29
New cards

Which function does a local variable belong to?

The function in which it was created

30
New cards

Can statements inside another function access a local variable?

No, an error will occur

31
New cards

What is the scope of a local variable?

The function in which it was created

32
New cards

Can different functions have local variables with the same name?

Yes, this allows multiple teams to collaborate and divide a project without having to coordinate variables used by the functions they author.

33
New cards

Can a function access another function's local variables?

No, each function does not see the other function's local variables

34
New cards

What is an argument?

A piece of data that is sent into a function

35
New cards

What is a parameter variable?

Variable assigned the value of an argument in a function call.

36
New cards

What is the scope of a parameter?

The function in which the parameter is used. Parameters are local variables defined by the function parameter list.

37
New cards

How are multiple arguments passed to a function?

Separated by commas in the parameter list.

38
New cards

What happens when changes are made to a parameter value within a function?

The argument remains unchanged.

39
New cards

What is a keyword argument?

An argument that specifies which parameter to pass the value to. An example is the sep and end parameters of the print() function.

40
New cards

Can keyword and positional arguments be mixed when calling a function?

Yes, but positional arguments must appear first.

41
New cards

What is a global variable?

A variable created outside all functions and can be accessed by any statement in the program file.

42
New cards

What must be done to assign a value to a global variable within a function?

The global variable must be redeclared within the function.

43
New cards

Why should the use of global variables be avoided?

They make debugging difficult and can cause dependency issues in functions.

44
New cards

What are global constants?

Global name that references a value that cannot be changed during the execution of the program.

45
New cards

Why do global variables make a program hard to understand?

They make the function hard to transfer to another program. They make the logical flow of the program confusing. They create issues when modifying and maintaining your source code.

46
New cards

How can you simulate a global constant in Python?

Create a global variable and do not re-declare it within functions.

47
New cards

What is a void function?

A group of statements within a program for performing a specific task.

48
New cards

When should you call a function?

When you need to perform a specific task.

49
New cards

What is a value-returning function?

A function that returns a value to the part of the program that called it.

50
New cards

What are standard library functions?

Pre-written functions that are included and installed with Python.

51
New cards

How are library functions viewed by programmers?

As a 'black box' that performs common tasks. You can call the function, but you may not have access to the source code of the function. A programmer can see the input and output but not the internal processing of the function.

52
New cards

What are modules in Python?

Files that store functions of the standard library and help organize them.

53
New cards

What is an import statement used for?

To call a function stored in a module.

54
New cards

What is the format of an import statement?

import module_name

55
New cards

Why are random numbers useful in programming?

They are useful in a lot of programming tasks. You may need a randomly generated number when programming simulations of games.

56
New cards

What does the random module include?

Library functions for working with random numbers. Includes functions like randint(), randrange(), and random().

57
New cards

What is dot notation used for?

To call a function belonging to a module.

58
New cards

What is the format of dot notation?

module_name.function_name()

59
New cards

What does the randint function do?

Generates a random integer in the specified range. The arguments include the start and end values of the range. Unlike the range() function, the ending limit is included in the range.

60
New cards

Where is the random number returned to?

To the part of the program that called the function.

61
New cards

What can the returned integer from randint be used for?

Anywhere that an integer would be used.

62
New cards

How can you experiment with the randint function?

You can learn about the randint() function by experimenting with it in interactive mode.

63
New cards

What is the randrange function?

Returns randomly selected integer from a sequence, unlike randint() function, the randrange() function does not include the ending limit in the range.

64
New cards

What are the arguments for the randrange function?

Same as the range function

65
New cards

What is the random function?

Returns a random float between 0.0 and 1.0

66
New cards

Does the random function receive any arguments?

No, it does not receive any arguments

67
New cards

What is the uniform function?

Returns a random float within a specified range

68
New cards

What are random number seeds?

Seed values that initialize the formula for generating random numbers

69
New cards

Why do we need to use different seeds?

To get different series of random numbers

70
New cards

What is the default seed value used by random functions?

System time

71
New cards

How can we specify a desired seed value?

By using the random.seed() function

72
New cards

How do we write a value-returning function?

Write a function and add one or more return statements

73
New cards

What is the format of a return statement?

return expression

74
New cards

What is returned to the calling part of the program?

The value of the expression in the return statement

75
New cards

Can the expression in the return statement be complex?

Yes, it can be a complex expression

76
New cards

In what situations can value-returning functions be useful?

Prompting user for input, simplifying mathematical expressions, complex calculations, using the returned value

77
New cards

What is an IPO chart?

A chart that describes the input, processing, and output of a function

78
New cards

What is the purpose of an IPO chart?

To design and document functions, displays the input, processing and output of a function.

79
New cards

How are IPO charts typically laid out?

In columns

80
New cards

What information is included in an IPO chart?

Brief descriptions of input, processing, and output

81
New cards

Can functions return strings?

Yes, functions can return strings

82
New cards

What are boolean values?

Values that are either true or false

83
New cards

What is a boolean function?

Returns True or False

84
New cards

How are boolean functions used?

To test conditions and simplify code

85
New cards

What are some common calculations that can be easily repeated using boolean functions?

Checking if a number is even

86
New cards

What is the format for returning multiple values in Python?

return expression1, expression2, etc.

87
New cards

What is the math module?

Part of the standard library for mathematical calculations

88
New cards

What is the purpose of the import math statement?

To use the math module in a program

89
New cards

What are the variables pi and e in the math module?

Global Constants used as mathematical values for pi and e.

90
New cards

How can the constants pi and e be used in equations?

To use the constants pi and e from the math module, use dot notation in the same way you call the functions in the math module.

For example, circle area = radius*2 math.pi

91
New cards

What is modularization?

Grouping related functions in modules

92
New cards

What are the benefits of modularization?

Easier understanding, testing, and maintenance of code

93
New cards

How can modularized code be reused?

By importing the module into different programs

94
New cards

What are the rules for module names?

Modules are files with filenames that end in .py, similar to source code files.

Module names cannot be a Python keyword.

95
New cards

What is a menu-driven program?

A program that displays a list of operations for the user to select

96
New cards

What is a menu?

List of options to call functions in your program and displayed as a list on the screen.

97
New cards

Why do you use a decision structure in a menu-driven program?

Used to determine the selected menu option and required operation.

98
New cards

What happens typically in a menu-driven program?

Based on the option selected, a function is called and executed.

The menu of options will be displayed until the user selects the option to quit the program.

Explore top notes

note
Chapter 4: Forces and Energy
Updated 1218d ago
0.0(0)
note
Unit 8: Ecology
Updated 95d ago
0.0(0)
note
Day 3
Updated 499d ago
0.0(0)
note
Chapter 13: Illicit Drugs
Updated 1092d ago
0.0(0)
note
excretory system notes
Updated 1176d ago
0.0(0)
note
Chapter 4: Forces and Energy
Updated 1218d ago
0.0(0)
note
Unit 8: Ecology
Updated 95d ago
0.0(0)
note
Day 3
Updated 499d ago
0.0(0)
note
Chapter 13: Illicit Drugs
Updated 1092d ago
0.0(0)
note
excretory system notes
Updated 1176d ago
0.0(0)

Explore top flashcards

flashcards
Flashcards pro zeměpis
80
Updated 316d ago
0.0(0)
flashcards
VET213 LAB END PRACTICAL SP2023
33
Updated 1064d ago
0.0(0)
flashcards
Criminology Unit 3
20
Updated 1234d ago
0.0(0)
flashcards
Econ Exam 2
66
Updated 1121d ago
0.0(0)
flashcards
W27 vocab (Klinh + Btram)
45
Updated 418d ago
0.0(0)
flashcards
El tiempo/ Que tiempo hace?
21
Updated 1204d ago
0.0(0)
flashcards
confirmation
32
Updated 980d ago
0.0(0)
flashcards
Speedtest Fische
25
Updated 62d ago
0.0(0)
flashcards
Flashcards pro zeměpis
80
Updated 316d ago
0.0(0)
flashcards
VET213 LAB END PRACTICAL SP2023
33
Updated 1064d ago
0.0(0)
flashcards
Criminology Unit 3
20
Updated 1234d ago
0.0(0)
flashcards
Econ Exam 2
66
Updated 1121d ago
0.0(0)
flashcards
W27 vocab (Klinh + Btram)
45
Updated 418d ago
0.0(0)
flashcards
El tiempo/ Que tiempo hace?
21
Updated 1204d ago
0.0(0)
flashcards
confirmation
32
Updated 980d ago
0.0(0)
flashcards
Speedtest Fische
25
Updated 62d ago
0.0(0)