Chapter 7: Functions and Modules Flashcards

1
Q

What is the syntax for defining your own function?

A
def functionName(list of parameters): 
       code detailing what the function should do 
      return [expression]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the syntax

A

def tells the program that indented code from next line onwards is part of the program. Return is the keyword used to return an answer from the function.

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

Can there be more than 1 return statement in a function?

A

Yes

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

What happens once the function executes a return statement?

A

It exits

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

What to do if the function need not return any value?

A

We can omit the return statement. Alternatively, we can write return or return none.

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

Write the script for defining a function.

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

What happens if the local variable shares the same name as the global variable?

A

Any code inside the function is accessing the local variable. Any code outside the function is accessing the global variable.

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

Write a script with the result showing the last flashcard

A

Result:

INSIDE THE FUNCTION
Local variable(shares the same name as a global variable)
OUTSIDE THE FUNCTION
Global variable(shares same name as a local variable)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does Python allowing a default value mean?

A

This means that Python allows to assign default values to the parameters when defining a function

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

Give an example for defining the default parameters

A
def someFunction(a,b,c=1,d=2,e=3):
print(a,b,c,d,e)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are salient features to remember when assigning default parameters?

A

1) All parameters with default values must be placed at the end of the parameters list.

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

How does Python allow us to pass a variable number of arguments to a function?

A

By using the *(asterisk) before the parameter

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

Give an example of variable length argument

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

When we add an asterisk in front of a parameter name, we can pass in a variable number of arguments to the function. This is known as a ________variable length argument list.

A

non-keyworded

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

If we want to pass in a keyworded variable length argument list to the function, we can use _______

A

double asterisks

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

Write a script defining a function with double asterisks

A
17
Q

If our function uses a normal argument (also known as a formal argument), a non-keyworded variable length argument list and a keyworded variable length argument list, we must define the function using the following order:

A

def someFunction2(farg,*args, **kwargs):

18
Q

What is the first way to import the entire module?

A

by writing
importmoduleName

for example import random

19
Q

How would you use the randrange() function in the random module?

A

We write random.range(1,10)

20
Q

What is the third way to import modules?

A

To import specific functions

21
Q

How can you import specific functions?

A

By writing

moduleName import name1 [,name2[,…nameN]]

22
Q

What would you write to import the randrange() function from the random module?

A

from random import randrange

23
Q

What if we want to import more than 1 function?

A

We separate them with a comma.
For example,
from random import randrange, randint

and then just write

randrange(1,10)

24
Q

What if you find it troublesome to write random each time you use the function?

A

you simply write r.randrange(1,10)

25
Q

Why should you create your own module?

A

If you want to reuse them in other programming projects in future

26
Q

How would you use the checkIfPrime() function defined earlier in another script?

A

Save the script with this function in the same folder as the another script which you are using to call on this function. The other script is called usecheckifprime.py