Learn Python in one minute | Python functions (on)

function

Functions are organized, reusable, code segments used to implement single or related functions.

In a python program, you need to define (declare) the function before using it, and then you can call it. As long as the necessary parameters are passed to the function in the form of the function definition, it can be called to complete the relevant function and obtain the function return result.

Define function

The syntax format of the definition function is as follows

def<Function name>(parameter list)<Function statement>return return value#This item is not required

By default, parameter values and parameter names are matched in the order defined in the function declaration

The code example is as follows

def hello():print("Hello World")hello()

The result is as follows

Hello World
>>>

Function call

The call of a function is to use a function. Defining a function is equivalent to giving the function a name, specifying the parameters contained in the function, and the code block structure.

After the basic structure of this function is completed, you can execute it through another function call or directly from the Python command prompt

The following is an example of function call

def printme( str ):
 # Print any incoming string
 print(str)return
 
# Call functions
printme("Hello")printme("World")

The output of the above example

Hello
World

Function parameters

In a python program, parameters are an important element of a function. When calling a function, parameters can be passed or not passed. In some cases, the number of parameters in a function cannot be determined. The parameters of the function will be explained in detail below.

Formal and actual parameters

The formal parameter represents a piece of information required by the function to complete its work, and the actual parameter is the information passed to the function when the function is called

The following are the formal parameter types that can be used when calling a function:

※ Required parameters

※ Keyword parameter

※ Default parameters

※ Variable length parameter

Required parameters

Required parameters are also called positional arguments and must be passed into the function in the correct order. The number when called must be the same as when declared.

The following example calls the printme() function, you must pass in a parameter, otherwise a syntax error will occur:

# Writable function description
def printme( str ):"Print any incoming string"print(str)return
 
# Call the printme function, no parameters will be reported
printme()

Example output results:

Traceback(most recent call last):
 File "test.py", line 10,in<module>printme()
TypeError:printme() missing 1 required positional argument:'str'
Keyword parameters

Keyword parameters are closely related to function calls. Function calls use keyword parameters to determine the value of the passed parameter. The use of keyword arguments allows the order of the parameters in the function call to be inconsistent with the declaration, because the Python interpreter can match the parameter values with parameter names.

The following example uses parameter names when calling the function printme():

# Writable function description
def printme( str ):"Print any incoming string"print(str)return
 
# Call printme function
printme( str ="Hello World")

Example output results:

Hello World

The following example demonstrates that the use of function parameters does not require a specified order:

# Writable function description
def printinfo( name, age ):"Print any incoming string"print("first name: ", name)print("age: ", age)return
 
# Call printinfo function
printinfo( age=50, name="Tom")

Example output results:

first name:  Tom
age:50

Recommended Posts

Learn Python in one minute | Python functions (on)
Learn Python in one minute | Object-oriented (Chinese)
Learn Python in One Minute | Object Oriented (Part 1)
Learn the hard core operation of Python in one minute
Learn about garbage collection in Python
Compare Excel, learn Python window functions
Python functions
Python review one
How to program based on interfaces in Python
One picture flow: all built-in exceptions in Python
One article to get regular expressions in Python
03. Operators in Python entry
Python beginners learn decorators
Join function in Python
12. Network Programming in Python3
print statement in python
Install Python3 on Ubuntu 14.04
Concurrent requests in Python
Install python in Ubuntu
Install Python3 on Ubuntu 16.04
Learn about Python3 coroutine
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Install Python3.7 on Ubuntu
python Tic-Tac-Toe-text version (on)
Computational Geometry in Python