Functions Flashcards
Function
Function code is not executed when defined
A function is defined using the “def” keyword
Function once defined can be invoked
If function in a function further indentation
Functions as objects-doc access to documentation
Keyword Argument
values are assigned to Keyword arguments by variable names during function invocation
Keyword arguments can be specified out of order
Easier to maintain code since the value of each argument is clearly seen during invocation
Keyword arguments are required arguments unless default values are specified
Key word arguments will be specified after positional arguments
Arguments are referred by variable names
Keyword arguments finds bugs
Positional Argument
Order is important at the time the function is invoked
Arguments are not referred to by variable names
Order of values match order of argument
unless default values are specified, positional arguments are required arguments
Positional arguments should be specified before keyword arguments
Input arguments
Input arguments can be keyword argument or positional argument
Info can be processed into function using input arguments
input arguments are specified at the time of function definition
input arguments can be of primitive or complex data type
Global variables
variables outside of function
Types of arguments function can accept
variable length argument
default argument
keyword argument
Variable length positional arguments
parsed into function as Tuple
some_fn (*args)
They can be of any data type- primitive or complex types
Function can accept any number of positional arguments
positional arguments follow keyword arguments
variable length keyword arguments are passed into function as dictionary
input arguments are of two types
keyword argument
positional argument
Consider the following bit of code. What will be the result of executing this bit of code?
x = 3
y = 4
def add(a, b):
result = x + y
print(result)
add(10, 20)
7
Consider a function definition which looks like this:
def some_function(a, b, c):
print(a, b, c)
Which of the following function invocations are correct?
some_function(2, 3, “Hello”)
some_function(2, 3, 4)
What is the default return value from a function when no return statement is specified?
None
Which of the following statement(s) about return values is/are false?
A function has to have a return statement
Not selected
A function is limited to having exactly one return statement
Selected
A function with input arguments cannot have a return statement
Selected
valid function names in Python
_123_Function_Name()
functionName()
_function_name()
- function name should not start with hyphen or number
def some_fn(a, b, c=True):
print(a, b, c)
a and b are required arguments, c is optional