Definition of Python function, parameter passing in and invocation of function

Source: Internet
Author: User

As an abstract way of computer code, functions play a very important role in Python. Today we introduce the definition of Python functions, the introduction of parameters, and how to invoke them. Where the function parameters are passed in in this section is the key content. The parameters of the Python function include required parameters, default parameters, mutable parameters, named keyword parameters, and keyword parameters. Five types of parameters can be passed in individually or in combination.


Definition of Python function

Python uses the DEF statement to define the function, then writes out the function name, parentheses, the arguments in parentheses, and the last colon that cannot be forgotten, and the body of the function needs to be written in an indented block on a different line, and the last return value is executed with the return statement. See an example of a self-coding function that asks for absolute values:

def f_abs (x):
if x >=0:
return X
return -X

Of course, if you have saved F_abs as a file through Pycharm, you can use CMD to start Python in the current file directory and then directly import the function module to invoke the F_abs function:

F:\>CD Pythoncode>python
>>> from abstest import f_abs
>>> F_abs (-1.0923)

In addition, when we write the function, we can also check the parameter number and parameter type of the function, and the function can return multiple values, but it is essentially a tuple.


Parameters of the function are passed in

The flexibility of function arguments makes Python functions very powerful, with the exception of the required parameters, including default parameters, variable parameters, named keyword parameters, and keyword parameters. function parameter Pass order is required parameter > default parameter > Variable parameter > named keyword parameter > keyword parameter.

Required Parameters:

First define a function that squares:

def Power (x):
return x*x
Power (-10)

It is convenient to define the square function, but if we want to ask for the higher power of X, I'm afraid this function will have to be changed, we need two required parameters to get it done:

def Power (x,n):
s = 1
while n >0:
n = n-1
s = s*x
return S
Power (10,3)

Default parameters:

When we redefine the power function, the original function that passed in only one of the required parameters has been invalidated, and we need the default parameter to help, defining the value of the parameter n as the default value of 2:

def Power (x,n=2):
s = 1
while n >0:
n = n-1
s = s*x
return S

>>> Power (10)
>>> Power (10,2)

From the above example we can see that the use of default parameters can simplify the invocation of functions. Required parameters before, the default parameters in the after, when more than one parameter, the change of large parameters in front, the change of small parameters can be used as the default parameters, the most important thing is that the default parameters must point to the immutable object! This is what you need to be aware of when setting default parameters.

Variable parameters:

We define a function for the sum of squares of a set of numbers, i.e. a2+b2+c2+ ... With mutable parameters we can write functions:

def Jisuan (*numers):
sum = 0
for N in numbers:
sum = SUM + N * n
return sum

The variable parameter visible by the above command simply needs to precede the parameter with an * number to complete its definition, and the parameter numbers accepts the data type as a list or tuple. When we have a list or a tuple, we can directly use it as a variable parameter in the function:

>>> nums = [2,3,5]
>>> Jisuan (*nums)

Keyword parameters:

Unlike a mutable parameter, which can pass in any parameter, the keyword parameter can pass in any parameter with a parameter name, so the Key-value format is apparently passed in as a dict data type. And look at the example:

def player (NAME,TEAM,**KW):
Print (' name: ', Name, ' Team: ', Team, ' other: ', kw)
Player (' Harden ', ' rockets ', city = ' Houston ')
Name:harden team:rockets Other: {' City ': ' Houston '}

The obvious point is that the keyword parameter increases the flexibility of the function, except for the required parameter, which allows the user to enter any desired parameter through the keyword argument. Similar to mutable parameters, we can also define a dict and then pass it as a keyword parameter into the function:

>>> info = {' City ': ' Golden States '}
>>> player (' Curry ', ' worriors ', **info)
Name:curry team:worriors Other: {' City ': ' Golden States '}

Named keyword parameters:

with the keyword argument, we can pass in any unrestricted argument, but if we want to limit the name of the keyword argument, you can use the named keyword argument, for example, only accept city and age as the keyword parameters, defined as follows:

def palyer (name,team,*,city,age):
Print (Name,team,city,age)

The named keyword parameter requires a * as a separator, and the following parameters are named keyword parameters, which are very different from the * * of the keyword parameter. However, if the function already has a mutable parameter before that, then the definition of the named keyword argument does not require * as a delimiter. Examples are as follows:

def palyer (name,team,*tec,city,age):
Print (Name,team,tec,city,age)

It is important to note that the named keyword parameter must be passed in to the function along with the parameter name, otherwise python will error.

>>> player (' Parker ', ' spurs ', city = ' San Antonio ', age = 32)
Parker Spurs San Antonio 32

One point to note is that the various parameters can be combined into the function, just need to pay attention to the order of incoming: Required parameters > Default parameters > Variable parameters > named keyword parameters > keyword parameters.


Call to function

Once we have defined the function, the corresponding function call is very simple, and the call can be done directly using the name of the functor. Of course, not every function needs us to customize, Python basic modules and various libraries have built in a lot of functions, we can call directly, this is similar to R. See some examples of Python functions:

>>> ABS (-0.3)
>>> Max (3,0,8,3)
>>> Int (10.24)

Definition of Python function, parameter passing in and invocation of function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.