2

I heard that argument means the method arguments, ( for example in below code)

def abc(a, b):
    pass

c = 2
d = 3
abc(c,d)

a and b are arguments and c and d are parameters. Correct me if I am wrong?

Then in the below code

def abc(c=1,d):
    pass

The error message is:

SyntaxError: non-default argument follows default argument

And also in below code

def abc(a,b):
    pass

abc(a=1,2)

The error message is:

SyntaxError: non-keyword arg after keyword arg

In both error messaeges, 'no-default argument follows default argument' and 'non-keyword arg after keyword arg', why there is no mention of paramter?

It caused the confusion to me many times with both the error messages so aksing this question. Is there any specific reason for error message to be like this. and also could you let me know how to remember the correct error message corresponding to that error. Thank you.

Kalyan Kumar
  • 111
  • 13
  • 3
    I've always heard the opposite: parameters are the things in the declaration and arguments are the things you pass at the call-site. But it doesn't really matter; so many people use the terms interchangeable today that it really only matters if you're a language specification designer. – Silvio Mayolo Jan 01 '18 at 07:38
  • I always remember it from *parametric equations*. Not sure if it makes a whole sense. – Yakup Türkan Jan 01 '18 at 08:07

2 Answers2

3

whenever you call a function suppose

def f1(a, b):
    pass

x = 10
y = 20

f1(x, y)

the above values 'x', 'y' are arguments and 'a', 'b' are parameters. During function/method calling those variables are called as arguments because you can pass anything like 'x', 'y' or maybe 'p', 'q' etc. But at the function definition those variables are called parameters because they would just be for that function only that is like a limit to that function.

In Python there is concept of position arguments, default arguments, non-default arguments, keyword arguments and named arguments.

def f2(a, b=10):
    pass

x = 10
y = 20

f2(x)
f2(x, y)

Here in above function 'b' is a default parameter its value by default would be 10, if whenever the function f2 is called and only 1 value of 'a' is going to come up like f2(x) and variable would 'b' be 10 and for 'a' = 10. And in the f2(x, y) the f2 function's parameters 'a', 'b' will be 10, 20 respectively.

Always remember that at function call position arguments come first then keyword/named parameters must be later on, because the position matters. Same at the function definition first non-default parameters then default ones. The non-defaults one would correspond to positional ones and rest one would correspond to remaining depending on your function call.

At case 1 of function call f2(x), here 'x' is the positional argument during function call and 'a' at the function definition is positional parameter and 'y' is the default parameter.

At case 2 of function call f2(x, y), both 'x' and 'y' are positional arguments and at function definition 'a' and 'b' both are positional parameters now, as 'y' would be passing value 20 to parameter 'b'.

The default value of any function definition would be took only if no argument is passed for that parameter at any function call like case 1 f1(x) or f1(p) then default would be took that is 'b' = 10 and 'a' would be 'x' or 'p'.

Next suppose,

def f3(a, b):
    pass

f3(a=5, b=15)
f3(b=100, a=200)

the above call f3, its arguments 'a' as 5 and 'b' as 15 are keyword/named arguments because they have name/key associated for its value. 'a' is linked as a name/key to value 5 and same for 'b' as 'b' is linked as a name/key to value '15'. Same you can think of like a dictionary.

Here during function call make sure names/keys must be same as function definition else we would get an error. Also ensure that no repetition of same arguments/parameters at function call or at function definition will also give us error.

And you can specify these keyword/named based arguments in any order. like in case 2 we have f3(b=100, a=200). At function definition the name/key 'a' would correspond to value 200 and the name/key 'b' would correspond to value 100.

def f4(a=1, b=2):
    pass

f4()
f4(b=20)
f4(a=10)
f4(a=100, b=200)
f4(b=50, a=75)

Here in f4 function definition 'a' and 'b' are having default values 1 and 2.

At case 1 the defaults values would be took.

At case 2 only 'b' as name/key value 20 is passed which would correspond to 'b' at function definition, 'a' and 'b' would be 1, 20 respectively.

At case 3 only 'a' as name/key value 10 is passed which would correspond to 'a' at function definition, 'a' and 'b' would be 10, 2 respectively.

At case 4 both 'a' and 'b' as passed as name/key values 50, 75 which would correspond to both 'a' and 'b' at function definition, 'a' and 'b' would be 75 and 50 respectively.

Remember that the name/key arguments only applies during function calling like f4(a=3, b=4). And during function definition def f5(a, b=20) here 'a' is non-default parameter or positional parameter, and 'b' is the default parameter as default value would be took if no value for 'b' is passed during the function call f5.

Xantium
  • 11,201
  • 10
  • 62
  • 89
Kaustubh.P.N
  • 229
  • 1
  • 8
0

Technically, parameters are variables in a function definition and arguments are the actual values given to the variables at the point of call. For example:

def foo(arg):
    return arg

foo('Python')

The variable arg in the function definition is the parameter and the value Python in the function call is an argument to that parameter.

Wikipedia has a nice description: Parameters and arguments

srikavineehari
  • 2,502
  • 1
  • 11
  • 21