Skip to content
Advertisement

Python Function parameters and arguments

I want to understand functions and arguments better, I’ve read and this is my implementation of what I have read.

I have 3 small functions to illustrate my point.

User input collection, grading function and main.

JavaScript

If I run the main function without its argument (1), it results in an error looking for the argument. I know that I declared in the function that it has a parameter (as i want this as a return value) but how would I go about calling the score function without its argument??
E.G

JavaScript

I imagine that functions are called without parameters and the arguments are passed into the parameters from the function definition (via user input)

Can someone provide an example as to how I could do this without needing the dummy variable (1) in the calling of the score function?

Advertisement

Answer

Here’s one way I would do it:

JavaScript

If you want to still be able to pass a parameter into the score function, like score(1), then you can instead assign an optional parameter to the function. Then, only read user input if num1 is not passed in to the score function.

JavaScript

Usage:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement