FUNCTIONS Flashcards

1
Q

How to define a function?

A
def hello():
      print('Howdy!')
       print('Howdy!!!')
       print('Hello there.')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does Define a function mean?

A

creating a function

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

What does calling a function mean?

A

Using function

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

what does passing a value mean?

A

giving a value to the function while calling it

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

What is an argument in a function?

A

a value being passed to the function is an argument

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

What is parameters?

A

Variables that have arguments assigned to them are parameters.

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

What is return value of a function?

A

it. In general, the value that a function call evaluates to is called the return value of the function.

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

What is return statement?

A

When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:

The return keyword
The value or expression that the function should return

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

An example of a fortune teller program?

A

import random

def getAnswer(answerNumber):
    if answerNumber == 1:
        return "It is certain"
    elif answerNumber == 2:
        return "It is decidedly so"
    elif answerNumber == 3:
        return "Yes"

r = random.randint(1,3)
fortune = getAnswer(r)
print(fortune)

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

What is None value?

A

which represents the absence of a value

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

What are keyword arguments?

A

Keyword arguments are often used for optional parameters.

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

What are print() keyword arguments?

A

print(‘the’,’cat’,’is’, sep =’ ,’,end =’ ‘ )

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

what is the Call Stack by an example?

A
def a():
       print('a() starts')
    ➊ b()
    ➋ d()
       print('a() returns')
   def b():
       print('b() starts')
    ➌ c()
       print('b() returns')
   def c():
    ➍ print('c() starts')
       print('c() returns')
   def d():
       print('d() starts')
       print('d() returns')

➎ a()

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

Parameters and variables that are assigned in a called function are said to exist in that function’s……..

A

local scope

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

Variables that are assigned outside all functions are said to exist in the ………..

A

global scope

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

variable that exists in a local scope is called a ……..

A

local variable

17
Q

a variable that exists in the global scope is called a ………

A

global variable

18
Q

when is a local variable created and destroyed?

A

A local scope is created whenever a function is called. Any variables assigned in the function exist within the function’s local scope. When the function returns, the local scope is destroyed, and these variables are forgotten.

19
Q

Why does scope matter?

A

Code in the global scope, outside of all functions, cannot use any local variables.
However, code in a local scope can access global variables.
Code in a function’s local scope cannot use variables in any other local scope.
You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.

20
Q

If you need to modify a global variable from within a function, use the ………
code example?

A

global statement

def spam():
  ➊ global eggs
  ➋ eggs = 'spam'
21
Q

How to prevent errors from stopping the program execution?

A

Errors can be handled with try and except statements. The code that could potentially have an error is put in a try clause. The program execution moves to the start of a following except clause if an error happens.

22
Q

give and example of try - except

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

print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))