Search code examples
pythonsyntaxdefault-value

Default Parameters in Function Definition, Syntax problems.


I am having an issue finding a straightforward answer to a question that I have.

I am coding a program that has some default values for certain parameters that do not end up being called by the user. My program is somewhat complicated so I decided to try a simplified problem.

def mult(x = 1, y = 2, z = 3):
    ans = x * y * z
    print(ans)

mult()

In this quick program, the function call would result in 6. Which makes sense because it goes to the default values I provided. My question is, how can I call this function if, for example, I wanted to define y and not any other variable? What would be the correct syntax in that situation.

My intuition was to call mult(x, 5, z) to indicate default values for x and z but a new value for y. I know that does not work and would like to know what the correct syntax would be.


Solution

  • You can specify the parameter to supply by using = at the call site:

    mult(y = 5)