0

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.

2
  • 1
    mult(y=7) .... Commented Aug 7, 2018 at 17:31
  • @JoranBeasley thanks for the clarification! Commented Aug 7, 2018 at 17:32

2 Answers 2

1

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

mult(y = 5)
Sign up to request clarification or add additional context in comments.

Comments

1

you can call it with keywords

mult(y=7) 

mult(z=55)

mult(z=12,y=16,x=5)

mult(x=15)

although as an aside its probably preferable to return ans instead of just printing it ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.