DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
kster
kster

Posted on • Updated on

Python Function Arguments

An argument is a value that is passed to a function when it is called. It might be a variable, value, or object passed to a function or method as input.

In python, there are 3 different types of arguments we can give a function.

Default arguments: arguments that are given default values

Positional arguments: arguments that can be called by their position in the function definition

Keyword arguments: arguments that can be called by their name.

Default argument

We can give a default argument by using an assignment operator =. This will happen in the function declaration.
Example:

def find_dinner_total(food, drinks, tip):
  print(food + drinks + tip =15)
Enter fullscreen mode Exit fullscreen mode

Our function is called find_dinner_total() which will calculate the total cost for dinner 🍽️. In our example our default argument is tip =15.
We can either choose to call the function without providing a value (this will result in the default value to be assigned) OR we can overwrite the default argument by entering a different value.

Positional argument

def find_diner_total(food, drinks, tip):
  print(food + drinks + tip)
Enter fullscreen mode Exit fullscreen mode

The first parameter passed is food, second drinks, and third is tip.
When our function is called, the position of the arguments will be mapped based on the positions that are defined in the function declaration.
Example:

# $70 total for food
# $30 total for drinks
# 15 tip
diner_bill(70, 30, 15)

Enter fullscreen mode Exit fullscreen mode

Keyword Arguments

In Keyword Arguments, where we refer to what each argument is assigned to in the function call.
Example:

dinner_bill(drinks=30, food=70, tip=15)
Enter fullscreen mode Exit fullscreen mode

Top comments (6)

Collapse
 
andromelus profile image
Florian CASTELAIN

I do not know which version of python you use, but having a default for an argument with other arguments without default value after it is not allowed (at least in 3.8):

Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def find_dinner_total(food, drinks, sales_tax=0.055, tip):
...     pass
...
  File "<stdin>", line 1
SyntaxError: non-default argument follows default argument
>>>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mellen profile image
Matt Ellen

You can have arguments without defaults before arguments with defaults, but not after.

def say(person, words='hello')
Enter fullscreen mode Exit fullscreen mode

is fine but

def say(person='world', words)
Enter fullscreen mode Exit fullscreen mode

Will throw the syntax error you show.

Collapse
 
kster profile image
kster Author

Thank you for mentioning this Matt!

Collapse
 
kster profile image
kster Author

I was not aware that you cannot have other arguments after default arguments. Thank you for mentioning this!

Collapse
 
andromelus profile image
Florian CASTELAIN

At least, run your code before posting it anywhere.

You’ve already scrolled down this far, why not join our community of 900k+ developers all learning together?