Module 5 - Functions cont'd Flashcards

1
Q

How do you activate a function?

A

function_name()

*MUST include the parentheses!

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

What is scope?

A

The place within a program where the variable is defined and can be accessed (local vs. global)

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

Where is local variable scope? Global variable scope?

A
Local = within the function in which they were created 
Global = entire program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can different functions have the local variables with the same name?

A

Yes, they don’t see each others’ variables

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

What is an argument?

A

A piece of data that is passed to the function when you call it:
Function_name(argument)

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

What gets assigned to the parameter variable when the function is called?

A

The argument value is assigned to the parameter variable when a function is called

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

What is the scope of the parameter?

A

The scope is the function in which the parameter is used

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

How do you create a global variable? Where can you access a global variable?

A

To create, write an assignment statement outside all the functions
Can be accessed by ANY statement in the program, including INSIDE functions

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

If a function needs to assign a value to the global variable, how do you do that?

A

The global variable must be REDECLARED within the function; so, within the function, type:
global variable_name

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

What is a global constant?

A

References a value that cannot be changed (e.g., pi)

To stimulate in python, create a global variable and DO NOT re-declare it within functions

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

Should you use global variables? why or why not?

A

No! Best practice is to NOT use them unless code won’t work without it

  1. Makes debugging difficult
  2. Can’t use functions in another program
  3. Program is hard to understand
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can the called function change a variable within the calling function?

A

No! You can pass a value or variable to the called function but you cannot change the variable’s value in the calling function because that variable is out of scope in the called function

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

What is a keyword argument and what is the syntax for it?

A

Argument that specifies which parameter the value should be passed to
function_name(parameter=value)

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

What’s the difference between void and value-returning functions?

A

Void functions will perform a task; they consist of statements
Value-returning functions will return a value to the part of the program that called it

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

What is the standard library?

A

Library of pre-written functions that comes with Python

Library functions have tasks that programmers commonly need (print, input, range, etc.)

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

What are modules?

A

Files that store the standard library’s functions

17
Q

T or F: Modules containing the related built-in functions are loaded into RAM when you start the interpreter.

A

False, some are loaded, some you must load by using the import statement (e.g., import math)

18
Q

What is the code for generating a random number between 1 and 5?
What is the data type of the return value?

A

import random
random.randint(1,5)
It returns an integer

19
Q

What does random.random() function do (after you’ve imported random module)?

A

Returns a random float between 0.0 and 1.0

does not receive arguments

20
Q

What does the range function do?

A

Generates the integer numbers between the given start integer to the stop integer
i.e., it returns a range object
(typically used in loops)

21
Q

What does the random.randrange function do?

A

Similar to range function, but returns randomly selected integer from the resulting sequence

22
Q

What does the random.uniform function do?

A

Returns a random float but allows user to specify

range

23
Q

T or F: Random number created by functions in random module are actually random.

A

False, they are pseudo-random numbers

24
Q

What initializes the formula that generates the random numbers?

A
Seed value
The random module uses the seed value as a base to generate a random number.
25
Q

What does the random module use if seed value is not present? How do you specify desired seed value?

A

It takes system current time

Can use random.seed()function to specify desired seed value

26
Q

What happens if you provide the same seed value before generating random data?

A

It will produce the same data

Need to use different seeds in order to get different series of random numbers

27
Q

How do you write a value-returning function?

A
def function_name(): 
\_\_stmt
\_\_return expression 

the value for expression will be returned to the part of the program that called the function

28
Q

What types of things can you return from a function to the part of the program that called it?

A

Literal, expression (can be complex), result of another value-returning function

29
Q

What is dead code?

A

Code that will never be executed (for example, after the ‘return’ statement in a function)

30
Q

What is a temporary variable? Why use it?

A

A variable used to store an intermediate value in a complex calculation.
They make debugging easier

31
Q

Define incremental development.

A

A program development plan intended to avoid debugging by adding and testing only a small amount of code at a time.

32
Q

What is scaffolding?

A

Code that is used during program development but is not part of the final version.
For example, print statements to check values

33
Q

Define guardian.

A

A programming pattern that uses a conditional statement to check for and handle circumstances that might cause an error.

34
Q

T or F: After a return statement runs, the function continues to execute any subsequent statements

A

False. As soon as a return statement runs, the function terminates without executing any subsequent statements.