Python Functions Flashcards

Learn about functions in Python

1
Q

Function

A

A named sequence of statements that performs a computation.

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

Function call

A

Evoking a function by its name.

*This is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the body of the function, runs the statements there, and then returns to where it left off.

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

Argument

A

The expression in parentheses after the name of a function.

*A function ‘takes’ an argument and ‘returns’ a result.

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

Return value

A

The returned result of a function.

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

When int() is used to convert floating-point values into integers, does it round off or omit the fraction?

A

int() omits the fraction of a floating-point value when converting it to an integer.

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

Module

A

A file that contains a collection of related functions.

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

Import statement

A

A statement used to import a module in order to use the functions therein.

*These statements create ‘module objects’

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

How do you access one of the functions defined in a module object?

A

Specify the name of the module and the name of the function, separated by a dot (dot notation)

math. pi
math. sin()
math. log10()
math. sqrt()

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

Function definition

A

Specifies the name of a new function and the sequence of statements that run when the function is called.

def NewFunction():
     print("I'm a good boy")

*Function definitions do not alter the flow of a program.

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

def

A

The keyword that indicates that this is a function definition.

  • The first character in the name of a function cannot be a number, and the name cannot be a keyword.
  • A function does not have to take any arguments.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Header

A

The first line of a function definition. It must end with a colon.

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

Body

A

The rest of a function definition following the header. The body must be indented.

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

Function object

A

Created when defining a function, it has type “function”.

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

Can you use a function inside another function?

A

Yes, if it has already been defined.

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

Statements inside a function are only run when the function is called. T/F?

A

True

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

Flow of execution

A

The order in which the statements run in.

  • Execution always begins at the first statement in a program, run one at a time, from top to bottom.
  • Function definitions do not alter the flow of a program.
17
Q

When reading a program, do you read from top to bottom, or follow the flow of execution?

A

Read a program following the flow of execution.

18
Q

Parameter

A

The name assigned to an argument passed to a function

def name(bruce):
    print(bruce)
  • This function assigns the argument to a parameter named ‘bruce’. When the function is called, it prints the value of the parameter. This works with any value that can be printed.
  • Any kind of expression can be used as a parameter:

def print(2 * 4 + math.cos(math.pi))

  • What we pass as an argument has nothing to do with the name of the parameter found in the function call.
  • Each parameter corresponds to the same value as its corresponding argument
19
Q

When is the argument evaluated in a function?

A

The argument is evaluated before a function is called, and only once.

20
Q

Variables can be used as arguments, T/F?

A

True.

21
Q

Local

A

A variable only exists inside the function that it is created in. When the function terminates, the local variables are destroyed.

*Parameters are also local.

22
Q

Stack diagram

A

Show the value of each variable, and the functions they belong to. This keeps track of where variables can be used.

Each function is represented by a frame, with the name of the function beside it and the parameters and variables of the function inside it.

23
Q

main

A

Special name given to the topmost layer of a program. Anything declared globally will belong to main.

24
Q

Traceback

A

List of functions that tells you what program file the error occurred in, what line, and what functions were executing at the time. It also shows the line of code that caused the error. The order is the same as a stack diagram, and the function currently running is at the bottom.