3 Functions Flashcards

1
Q

What are parameters? Where can you see parameters and arguments in a function?

A

argument = value passed to a function (e.g. ‘Al’)
parameter = variable that contains an argument (e.g. ‘name’)
➊ def sayHello(name):
print(‘Hello, ‘ + name)
➋ sayHello(‘Al’)

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

Functions are…

A

…the primary way to compartmentalize code into logical groups.

They are like black boxes: they have inputs in the form of parameters and outputs in the form of return values, and the code in them doesn’t affect variables in other functions.

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

Why are functions advantageous to have in your programs?

A

Variables in a function exist in their own local scope and do not affect other functions local scopes. This limits potential error sources and helps debugging.

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

When is a local scope created and when is it destroyed?

A

Function call: Local scope is created

Function return: Local scope is destroyed

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

If a function does not have a return statement, what is the return value of a call to that function?

A

The Default Value is None

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

How can you force a variable in a function to refer to the global variable?

A
def spam():
       global eggs
       eggs = 'hello'
-> Now eggs can be called outside the function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the data type of None?

A

NoneType value - it represents the absence of a vallue

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

How can you prevent a program from crashing when it gets an error?

A

With a Try and an Except clause

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

What goes in the try clause? What goes in the except clause?

A

Try: Code that could potentially have an error
Except: Code that defines what should happen if an error occurs

def spam(divideBy):
    try:
        return 42 / divideBy
    except ZeroDivisionError:
        print('Error: Invalid argument.')

print(spam(2))

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

How do you delay the output by 0.5 seconds?

A

import time

time.sleep(0.5)

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