All about Functions Flashcards
What keyword is used in python to define a function?
- def
- Example = def my_introduction():
What will be the output of the following function if it was invoked?
def my\_function(): print('Hello World')
It would result in a IndentationError
True or False:
Custom function names in python can start with a digit.
False
- Functions can start with either an underscore of a letter.
- The letter can be uppercase or lowercase
- Digits can be included after letters or the underscore
- Explain what is happening with the line “hi_function = my_function”
def my\_function(): print('Hello World')
hi_function = my_function
- Functions can be treated as objects
- hi_function is a variable which has a reference to the original function
- You can use either name to invoke the function.
What type of scope do the following variables have?
**name = 'Bryce' city = 'Toronto'**
def introduction():
print(‘Hi my name is: ‘ , name)
print(‘I live in : ‘ , city)
- Both variables have global scope
- Variables declared with global scope can be accessed by functions
How would you refer to the name variable on line 1.
- def introduction(name):
print(“Hello, my name is “ , name)
- Input argument
What will be the output when the following function is called
def my\_introduction(): print("Hello, my name is John")
my_introduction(‘Ted’)
- This would result in a TypeError as you’re trying to pass an argument to a function which doesn’t except an input argument.
True of False:
Input arguments passed into functions are limited to certain data types.
- False
- Input arguments can be of any data type, such as floats, ints, lists etc
When using multiple return values in a function, what is returned?
def add\_state(x,y): add\_result = x + y sub\_result = x - y
return add_result, sub_result
The function will return a tuple with the values as fields of the tuple.
maths = 100 is an example of what?
total_score(math=100, physics=56, chemistry=34, biology=99)
- A keyword argument
- Allows you to specify input arguments by name while invoking functions.
True or False
Python allows you to mix positional and keyword arguments to invoke a function.
- True
- However, the order does matter.
This will work:
total_score(100,56, chemistry=34, biology=99)
This will result in an SyntaxError
total_score(maths =100,56, chemistry=34, biology=99)
When invoking a function, do keyword arguments have to be in the correct order?
- No
- For example, this code is correct.
def total\_score(math, physics, chemistry, biology): print(math, physics, chemistry, biology)
total_score(biology=99, math=50, physics=89, chemistry=90)
What does *args do when used in functions?
*args indicates that the function can be invoked with any number of arguments.
def print_fn(*args):
Using the example below, what is **kwargs
def student_details(**kwargs)
- It stands for keyword variable length arguments
When you use **kwargs, what datatype does python pack the arguments into?
- Python packs the variable length arguments into a dictionary, not a tuple.
Which of the following are valid types of arguments in Python?
- Variable length arguments
- Ordered arguments
- Default arguments
- Keyword arguments
Correct answers are highlighted.
- Variable length arguments
- Ordered arguments
- Default arguments
- Keyword arguments
Which of the following are valid function names in Python?
- functionName()
- -functionName()
- _function_name()
- 123_Function_Name()
- _123_Function_Name()
- functionName()
- -functionName()
- _function_name()
- 123_Function_Name()
- _123_Function_Name()
What does this function indicate?
def some\_fn(a,b,c=True): print(a,b,c)
- a and b are optional arguments, c is required
- a and b are string arguments, c is boolean
- a and b are numeric arguments, c is boolean
- a and b are required arguments, c is optional
a and b are required arguments, c is optional
Which of the following statements about the data types of return values is/are false?
- A return statement is mandatory in functions
- A function in Python can return a dictionary
- A function in Python can return nothing
- A function in Python may have no return statement
A return statement is mandatory in functions
Which of the following function definitions allows the function to accept variable length arguments?
- some_fn(…args)
- some_fn(…)
- some_fn(*)
- some_fn(*args)
some_fn(*args)
Which of the following statements about functions is false?
- Functions can have different names
- Functions are invoked using parenthesis
- Functions can contain any number of statements in the function body
- Function cannot access variables which are declared outside the function
Function cannot access variables which are declared outside the function
What is the default return value from a function when no return statement is specified?
- ’’’
- 0
- -1
- None
None
Which of the following statements about positional arguments to function is/are true?
- A function can accept any number of positional arguments
- They can be of any data type