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:
def f_abs(x):
if x >= 0:
return x
else:
return -xIf the function is saved in a file (e.g., abstest.py), it can be imported and called from the command line:
C:\Users\Administrator>F:
F:\>cd pythoncode>python
>>> from abstest import f_abs
>>> f_abs(-1.0923)
1.0923Python 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:
def power(x):
return x * x
power(-10)
100When more arguments are needed, additional required parameters are added:
def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
power(10, 3)
1000Default parameters provide fallback values and must follow required parameters. Example:
def power(x, n=2):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
>>> power(10)
100
>>> power(10, 2)
100Variable parameters (using *) allow an arbitrary number of positional arguments:
def jisuan(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum
>>> nums = [2, 3, 5]
>>> jisuan(*nums)
38Keyword parameters (using **) accept any number of named arguments as a dictionary:
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'}Keyword arguments can also be passed via a pre‑constructed dictionary:
info = {'city': 'Golden states'}
player('Curry', 'Warriors', **info)
# Output: name: Curry team: Warriors other: {'city': 'Golden states'}Named keyword parameters restrict the allowed keyword arguments and require a * separator:
def player(name, team, *, city, age):
print(name, team, city, age)
player('Parker', 'Spurs', city='San Antonio', age=32)
# Output: Parker Spurs San Antonio 32When a function already has variable parameters, the * separator is not needed for named keyword parameters:
def player(name, team, *tec, city, age):
print(name, team, tec, city, age)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:
>> abs(-0.3)
0.3
>>> max(3, 0, 8, 3)
8
>>> int(10.24)
10Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
