Can You Specify Unlimited Number of Parameters for a Function Python
A Routine is a named group of instructions performing some tasks. A routine can always be invoked as well as called multiple times as required in a given program.
When the routine stops, the execution immediately returns to the stage from which the routine was called. Such routines may exist predefined in the programming linguistic communication or designed or implemented by the programmer. A Function is the Python version of the routine in a program. Some functions are designed to return values, while others are designed for other purposes.
We pass arguments in a function, we tin can pass no arguments at all, unmarried arguments or multiple arguments to a function and can call the function multiple times.
Example:
Python
def
displayMessage():
print
(
"Geeks for Geeks"
)
displayMessage()
Output:
Geeks for Geeks
In the higher up program, the displayMessage() function is chosen without passing any arguments to it.
Python
def
displayMessage(msg):
print
(
"Hello "
+
msg
+
" !"
)
msg
=
"R2J"
displayMessage(msg)
Output:
Hullo R2J !
In the above plan, the displayMessage() function is called past passing an argument to information technology. A formal argument is an argument that is present in the part definition. An actual argument is an argument, which is present in the part call.
Passing multiple arguments to a function in Python:
- We can pass multiple arguments to a python part past predetermining the formal parameters in the part definition.
Python
def
displayMessage(argument1, argument2, argument3):
impress
(argument1
+
" "
+
argument2
+
" "
+
argument3)
displayMessage(
"Geeks"
,
"four"
,
"Geeks"
)
- Output:
Geeks 4 Geeks
- In the higher up program, multiple arguments are passed to the displayMessage() function in which the number of arguments to be passed was fixed.
- We can pass multiple arguments to a python function without predetermining the formal parameters using the beneath syntax:
def functionName(*argument)
- The * symbol is used to pass a variable number of arguments to a role. Typically, this syntax is used to avert the lawmaking failing when nosotros don't know how many arguments volition exist sent to the function.
Python
def
calculateTotalSum(
*
arguments):
totalSum
=
0
for
number
in
arguments:
totalSum
+
=
number
print
(totalSum)
calculateTotalSum(
five
,
4
,
3
,
2
,
1
)
- Output:
15
- In the above plan, the variable number of arguments are passed to the displayMessage() function in which the number of arguments to be passed is not predetermined. (This syntax is only used to pass non-keyword arguments to the function.)
- Nosotros can pass multiple keyword arguments to a python role without predetermining the formal parameters using the below syntax:
def functionName(**argument)
- The ** symbol is used earlier an argument to pass a keyword argument dictionary to a role, this syntax used to successfully run the code when we don't know how many keyword arguments will be sent to the function.
Python
def
displayArgument(
*
*
arguments):
for
arg
in
arguments.items():
print
(arg)
displayArgument(argument1
=
"Geeks"
, argument2
=
4
,
argument3
=
"Geeks"
)
- Output:
('argument2', four) ('argument3', 'Geeks') ('argument1', 'Geeks')
- In the above programme, variable number of keyword arguments are passed to the displayArgument() office.
Hither is a programme to illustrate all the above cases to laissez passer multiple arguments in a function.
Python
def
displayArguments(argument1,
*
argument2,
*
*
argument3):
print
(argument1)
for
arg
in
argument2:
print
(arg)
for
arg
in
argument3.items():
print
(arg)
arg1
=
"Welcome"
arg3
=
"Geeks"
displayArguments(arg1,
"to"
, arg3, agr4
=
4
,
arg5
=
"Geeks !"
)
Output:
Welcome to Geeks ('agr4', 4) ('arg5', 'Geeks!')
The to a higher place program illustrates the use of the variable number of both not-keyword arguments and keyword arguments every bit well every bit a non-asterisk argument in a function. The non-asterisk statement is always used before the single asterisk statement and the single asterisk argument is e'er used before the double-asterisk statement in a function definition.
Source: https://www.geeksforgeeks.org/how-to-pass-multiple-arguments-to-function/
0 Response to "Can You Specify Unlimited Number of Parameters for a Function Python"
Post a Comment