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.