Functions Flashcards

1
Q

Function

A

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

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

Keyword Argument

A

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

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

Positional Argument

A

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

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

Input arguments

A

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

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

Global variables

A

variables outside of function

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

Types of arguments function can accept

A

variable length argument

default argument

keyword argument

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

Variable length positional arguments

A

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

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

input arguments are of two types

A

keyword argument

positional argument

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

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)

A

7

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

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?

A

some_function(2, 3, “Hello”)

some_function(2, 3, 4)

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

What is the default return value from a function when no return statement is specified?

A

None

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

Which of the following statement(s) about return values is/are false?

A

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

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

valid function names in Python

A

_123_Function_Name()

functionName()

_function_name()

  • function name should not start with hyphen or number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

def some_fn(a, b, c=True):
print(a, b, c)

A

a and b are required arguments, c is optional

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