Python Functions
Function is a block of code with independent functions encapsulated into a small module that can be called directly, thus improving the efficiency of the code writing and reuse, it should be noted that the function needs to be called before it will be executed, and call the function needs to be called according to the function name
1, the definition of the function format
def functionName()::
function code
2. Functions that use the current file
If we define a function and run the program, the function will not be called.
def hello(): print('hello')
3. Calling and defining functions
To execute a function, you need to call it by its name.
Defining Functions def hello(): print('hello') call function hello()
Note that in some languages, function calls can occur before the definition of the function, while in Python, function calls can only occur after the function's tini, if the function is called before the definition of the function, Python will not be able to find the function, thus reporting errors
4. Use of functions from other documents (modules)
For example, if we want to print the multiplication table in many places, we can encapsulate the print code into a function, and call the function directly where we want to use it.
Create a nine-nine-nine multiplication table.py file, the file defines a function priint_99(), the function is to print the nine-nine-nine multiplication table code
# Outputs five consecutive lines on the console*, each line with an increasing number of stars. def print_99(): # Define the number of lines row = 1 # Cycle 9 times, print 9 lines. while row <= 9: # of columns defined col = 1 while col <= row: # Every time the column loops, print row * col = (row * col), and cancel the line feeds. print('%d * %d = %d ' % (row, col, row * col), end='') # Increasing columns col += 1 # line prints a newline every loop print('') # Incremental rows row += 1
Next, create a functions.py file, import the multiplication table file (often called a module), and call the functions in the module.
# Import the class (module) in which the function resides. import multiplication table # Call function: module name. Function name multiplication table.print_99()
Run the function.py file, you can call the code in the function to print out the contents of the multiplication table.
5. Finding the sum of two numbers
Functions can be passed parameters and manipulated within the function, in the following format
def function name(parameter 1,parameter 2)::
function code
We define a function add(), which is used to find the sum of any two numbers.
# Define the summation function def add(num1, num2): print('%d + %d = %d' % (num1, num2, num1 + num2)) # Call the summation function add(1, 2)
When calling a function, just pass the number you want to operate on as an argument.
This article on the basis of the Python function is introduced to this article, more related Python function content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!