Robotics & Artificial Intelligence
What would be output of the following code snippet?
def multiply(x, y=2):
return x * y
result = multiply(5)
print(result)
- 5
- 10
- 2
- Error
Python Functions
2 Likes
Answer
10
Reason — Default arguments are used when a value is not provided during the function call. In the given function multiply(x, y=2), the parameter y has a default value of 2. When the function is called as multiply(5), the value 5 is assigned to x and the default value 2 is assigned to y. Therefore, the function returns 5 × 2 = 10, which is printed as the output.
Answered By
3 Likes
Related Questions
A function in Python is called by:
- using the "call" keyword followed by the function name and parameters
- using the "invoke" keyword followed by the function name and parameters
- using the function name followed by parenthesis and arguments
- using the "run" keyword followed by the function name and parameters
What is the purpose of return statement in Python functions?
- It is used to terminate the execution of a function
- It is used to define the output of a function
- It is used to import modules into a function
- None of these
In the following code snippet, what does 5 in the argument list represent?
def add(x, y=5): return x + y- return value
- default value
- input value
- None of these
Which keyword is used to define function with variable length arguments?
- *args
- **kwargs
- *var
- Both a and b