Do all parameters have to be used within a function?

Question

Will a function still work if there are parameters, but some of them are never used within the function itself?

Answer

Functions will still work as normal even if none of the parameters are actually used within the function code itself.

However, doing this may be counterintuitive, as the purpose of parameters is to allow different input values to be used when running a function to produce results based on the input. Not using some of the parameters means that although the input value must be provided in the function call, it’s not used, so it can be a waste of space or memory.

Example

# This function takes 3 parameters, but only uses 
# 2 of them in its code.
def add_two(a, b, c):
  return a + b

# The call still requires inputs for each parameter.
add_two(10, 30, 50) # returns 40
16 Likes

In the example, the function called is not the function defined.

9 Likes

@jephos249 can properly address this question. Would it were he could be so obliged.

In this function, why did you use return instead of print()?

Is there any advantage in using return?

1 Like

As you do more coding, you will find that you use print() less and less. print() is useful only for showing a result onscreen, but with return, the result can be passed to a variable or to another function.

The famous introductory “hello, world!” expression would be more akin to real-world usage as something like:

def hello(name):
    return "Hello + " " + name + "!"
# type your name within quotes in place of "world"
name = "world"
greeting = hello(name)
print(greeting)
17 Likes

Reading the replies, I see that I mistook the question and was expecting different replies. I misread it as “Do you have to use all the parameters when you call a function?” Does anyone have any insight on this?

I’m thinking of something like a command line tool, where you can add -letters or --commands. I suppose, though, that this might answer my own question. You could hypothetically design a program that potentially uses more parameters, but they are optional.

1 Like

you got the wrong code.

Here is the correct code that will return 40:

def some_numbers(a, b, c):
	print(a + b)

some_numbers(10, 30, 50)

Make sure you run your code in compiler prior to posting it.

Originally, the example code contained a mistake which would result in a NameError when attempting to run the code. Thank you, @ghulseman and @ajax8473289299 for noticing, and pointing it out. The mistake has been corrected.

Do All parameters have to be used in a function call? It looks like yes they do

def mult_x_add_y(number, x, y):
  print(number*x + y)
mult_x_add_y(10,2)

When I intentionally leave out the last parameter in the call i get an error: TypeError: mult_x_add_y() missing 1 required positional argument: ‘y’

Hello, @dkirkeberg.

Since none of the functions parameters are optional, the arguments are required. The original post, however, refers to the parameters being used inside the function. This code would execute for example:

def mult_x_add_y(number, x, y):
    print(x + y) #didn't use number

mult_x_add_y(10, 2, 4) #prints 6

This would also execute:

def mult_x_add_y(number, x, y = 4): #if an argument is supplied for y it gets assigned, otherwise, the default value of 4 is assigned
    print(x + y) #still not using number, and y is now optional

mult_x_add_y(10, 2) #prints 6
mult_x_add_y(10, 2, 15) #prints 17

Happy coding!

8 Likes

To complement @midlindner’s examples, we have, as would be expected, variants, and also as expected, inherent provisos. The last can be demonstrated thus,

def mult_x_add_y(n, x = 1, y = 0):
    return n * x + y

When we multiply by zero we get zero, hence we have a default value of 1 to permit the multiplication, albeit there is no change. By the same token we add zero, again so there is no change. The return value is an identity of the primary argument, n when the defaults are in effect.


>>> def foo(*args):
	print (type(args))

	
>>> foo(1,2,3,4,5,6,7,8,9,0)
<class 'tuple'>
>>> 

Above we see that any length argument sequence is cast as a tuple.

>>> def foo(*args):
	print (args)

	
>>> foo(1,2,3,4,5,6,7,8,9,0)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
>>> 

Though it mightn’t be so advisable to venture away from the uniform dynamics of the varying inputs, with careful design we can glean out the singular variables we want from the front of the list and lump the rest into another tuple.

>>> def foo(*args):
    a = args[0] or None
    b = args[1] or None
    c = args[2] or None
    d = args[3:]
    print(a,b,c,d)

    
>>> foo(1,2,3,4,5,6,7,8,9,0)
1 2 3 (4, 5, 6, 7, 8, 9, 0)
>>> 

But we need to go back…

>>> def foo(*args):
	print (args)

	
>>> foo()
()
>>> foo(10)
(10,)
>>> 

Nothing in, and a tuple is still returned. A single argument and a tuple is returned with a comma after the value. Why? (10) is not a tuple; (10,), is.

>>> foo(10,0)
(10, 0)

No more added comma. The above is unquestionably a tuple.

>>> foo(10,0, 0)
(10, 0, 0)
>>> foo(10,0, 0, 0)
(10, 0, 0, 0)
>>> foo(10,0, 0, 0, 0)
(10, 0, 0, 0, 0)
>>> 

Back to the above,

>>> def foo(*args):
    a = args[0] or 0
    b = args[1] or 1
    c = args[2] or 0
    d = args[3:]
    print(a, b, c, d)

    
>>> foo(0,0,0,0,0,0,0,0,0,0)
0 1 0 (0, 0, 0, 0, 0, 0, 0)
>>> 

Change the variable names of a, b, and c, and we can return just the result we want, or the result and the residual data if we have some further use of it. Recall that it was mentioned that careful design would be involved.

>>> def foo(*args):
    n = args[0] or 0
    x = args[1] or 1
    y = args[2] or 0
    d = args[3:]
    print(n,x,y,d)
    return n * x + y

>>> foo(0,0,0,0,0,0,0,0,0,0)
0 1 0 (0, 0, 0, 0, 0, 0, 0)
0
>>> 
>>> foo(10)
Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    foo(10)
  File "<pyshell#45>", line 3, in foo
    x = args[1] or 1
IndexError: tuple index out of range
>>> 
>>> foo(10, 0)
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    foo(10, 0)
  File "<pyshell#45>", line 4, in foo
    y = args[2] or 0
IndexError: tuple index out of range
>>> 
>>> foo(10,)
Traceback (most recent call last):
  File "<pyshell#49>", line 1, in <module>
    foo(10,)
  File "<pyshell#45>", line 3, in foo
    x = args[1] or 1
IndexError: tuple index out of range
>>> 
>>> foo(10,0,0,0)
10 1 0 (0,)
10
>>> foo(10, 5, 5)
10 5 5 ()
55
>>> foo(7, 5, 7)
7 5 7 ()
42
>>> 

Mull this over. It bears discussion at some point. If now is not the time, then it should be some time soon.

1 Like

Is there any way that I used 3 parameters but only want to add value of any two and put another “Null” value

you can assign x to 1

def mult_x_add_y(number, x, y):
print(number * x + y)
mult_x_add_y(1, 1, 1) #results 2

1 Like

Do all parameters have to be used within a function?
The answer is yes, function will work even if some or all of the parameters are not used, but it could be a downside as it will lead to consumption of space and memory, which is a waste of space and memory in fact. So, it can be done but it is not to good or efficient to use.

Which would be minimal, since they are only parameters and garbage collection will recover the memory of any function that has been exited.

mtf…I was following your post feed here and got clear understanding of your flow until this part that I “quoted” above. Can you be so kind to explain your reasoning in the source code and the interpreter outputs?

When I read your source code “n = args[0] or 0” I interpret it as booliean logic…True or False. Here I would have thought “n” would be set to “False” not “0”. And…“x = args[1] or 1” where x would be set to “True” not “1”.

Am I missing something here?

Also in your “IndexError’s” it says that your tuples are out of range…“foo(10)” has a range from 0-9 which is length 10. That is confusing me as well.

Bottom line, this was demonstration code, only, and ages ago. What can we glean from it, as it stands?

Indeed, your code was just for demonstration purposes. I would like to pick the brain of a master in diguise.

After carefully rummaging through the “IndexError’s” I realized that you did not pass a “range” function for the argument just “one” argument and that did not compile correctly in your “def” function.

But what I would like to grasp, is the first portion the code that blew my hair back when reading it. I “quoted” it below.

    0 1 0 (0, 0, 0, 0, 0, 0, 0)
//  n x y  ...d

So are these assigment statements evaluating to boolean expressions? Why are the outputs of these variables n = 0, x = 1, and y = 0…? Shouldn’t they be n = False, x = True, and y = False? Sorry if I’m being obtuse.