FUNCTIONS Flashcards
How to define a function?
def hello(): print('Howdy!') print('Howdy!!!') print('Hello there.')
What does Define a function mean?
creating a function
What does calling a function mean?
Using function
what does passing a value mean?
giving a value to the function while calling it
What is an argument in a function?
a value being passed to the function is an argument
What is parameters?
Variables that have arguments assigned to them are parameters.
What is return value of a function?
it. In general, the value that a function call evaluates to is called the return value of the function.
What is return statement?
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
An example of a fortune teller program?
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)
What is None value?
which represents the absence of a value
What are keyword arguments?
Keyword arguments are often used for optional parameters.
What are print() keyword arguments?
print(‘the’,’cat’,’is’, sep =’ ,’,end =’ ‘ )
what is the Call Stack by an example?
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()
Parameters and variables that are assigned in a called function are said to exist in that function’s……..
local scope
Variables that are assigned outside all functions are said to exist in the ………..
global scope
variable that exists in a local scope is called a ……..
local variable
a variable that exists in the global scope is called a ………
global variable
when is a local variable created and destroyed?
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.
Why does scope matter?
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.
If you need to modify a global variable from within a function, use the ………
code example?
global statement
def spam(): ➊ global eggs ➋ eggs = 'spam'
How to prevent errors from stopping the program execution?
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.
give and example of try - except
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))