Understanding Python Functions: Definitions, Parameters, and Calls
This article explains how to define and use Python functions, covering required, default, variable, keyword, and named keyword parameters, with detailed code examples for function definition, parameter passing, and invocation, as well as notes on return values and best practices.
As an abstract way of representing code, functions play a crucial role in Python. This section introduces function definition, parameter passing, and invocation, focusing on the various parameter types.
Python uses the def statement to define a function, followed by the function name, parentheses with parameters, a colon, and an indented body. Example:
<code>def f_abs(x):
if x >= 0:
return x
else:
return -x
</code>If the function is saved in a file (e.g., abstest.py ), it can be imported and called from the command line:
<code>C:\Users\Administrator>F:
F:\>cd pythoncode>python
>>> from abstest import f_abs
>>> f_abs(-1.0923)
1.0923
</code>Python functions can return multiple values (as a tuple) and can perform argument type checks.
Required parameters must be provided in order. Example of a simple square function:
<code>def power(x):
return x * x
power(-10)
100
</code>When more arguments are needed, additional required parameters are added:
<code>def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
power(10, 3)
1000
</code>Default parameters provide fallback values and must follow required parameters. Example:
<code>def power(x, n=2):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
>>> power(10)
100
>>> power(10, 2)
100
</code>Variable parameters (using * ) allow an arbitrary number of positional arguments:
<code>def jisuan(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum
>>> nums = [2, 3, 5]
>>> jisuan(*nums)
38
</code>Keyword parameters (using ** ) accept any number of named arguments as a dictionary:
<code>def player(name, team, **kw):
print('name:', name, 'team:', team, 'other:', kw)
player('Harden', 'Rockets', city='Houston')
# Output: name: Harden team: Rockets other: {'city': 'Houston'}
</code>Keyword arguments can also be passed via a pre‑constructed dictionary:
<code>info = {'city': 'Golden states'}
player('Curry', 'Warriors', **info)
# Output: name: Curry team: Warriors other: {'city': 'Golden states'}
</code>Named keyword parameters restrict the allowed keyword arguments and require a * separator:
<code>def player(name, team, *, city, age):
print(name, team, city, age)
player('Parker', 'Spurs', city='San Antonio', age=32)
# Output: Parker Spurs San Antonio 32
</code>When a function already has variable parameters, the * separator is not needed for named keyword parameters:
<code>def player(name, team, *tec, city, age):
print(name, team, tec, city, age)
</code>Parameter order must follow: required > default > variable > named keyword > keyword.
Function calls are straightforward: use the function name with appropriate arguments. Built‑in functions such as abs , max , and int can be called directly:
<code>>> abs(-0.3)
0.3
>>> max(3, 0, 8, 3)
8
>>> int(10.24)
10
</code>Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.