6 Defining Functions Flashcards

1
Q

How to import all the functions in a library?

A

from function import *

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

What are the two drawbacks of not using functions?

A

having to write the code twice, the same code has to be maintained at two different places

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

What is “a function definition”?

A

A part of the program to creates a function

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

What does it mean for a function to be “called” or “invoked”?

A

The function is used in a program

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

What is “the scope of a variable”?

A

Refers to the places in a program where a given variable may be referenced

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

Discuss the scope of variables used inside a function.

A

They are local to that function.

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

What is only way for a function to see a variable from another function?

A

Passing the variable as a parameter

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

How does “a function definition” look like?

A

def ():

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

What are “formal parameters”?

A

They are variables only accessible in the function body (subset of local variables).

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

How to call a function?

A

()

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

What happens after a function call (five steps)?

A

(1. Calling program suspends execution.)(2. Function definition is looked up.)(3. Formal parameters get assigned by values of actual parameters.)(4. The function body is executed.)(5. Control returns to the point of function call.)

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

How are formal and actual parameters matched?

A

By position

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

How to use “keyword parameters”?

A

def ( = ): , ( = ), match my name

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

Where do fundamental ideas and vocab for functions come from?

A

From mathematical functions

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

What two things happen after control reaches “return” statement?

A

(1. exits and returns to the point of function call.)(2. The value provided in the return statement is sent back to caller.)

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

In what form is the value in return statement returned to caller?

A

As expression

17
Q

Why are value-returning functions extremely useful?

A

They are more elegant and flexible.

18
Q

How to print in a file?

A

(outf = open(‘something.txt’, ‘w’))(print(something, file=outf))(outf.close()

19
Q

How to return more than one value?

A

return ,

20
Q

How to achieve simultaneous assignment?

A

, = return ,

21
Q

What do “functions without a return” return?

A

None

22
Q

What is the main way to send information from a function back to its caller?

A

Return values

23
Q

Does the called function have access to the variable that holds the actual parameter?

A

No. The formal parameter only receive the values of actual parameters.

24
Q

Does python pass all parameter by value?

A

Yes

25
Q

Why can lists be changed inside a function?

A

Because a list is mutable, that is, it is only the vehicle to values, not the actual values themselves

26
Q

Why are functions used even when they sometimes make programs longer?

A

Because they make programs more modular.