How to use function def in python

A function in python is a reusable block of code used to achieve a certain function.

Functions are created with the def keyword, and the syntax structure is as follows:

def functionname( parameters ):
   "函数_文档字符串"
   function_suite
   return [expression]

The following defines a function that implements the addition operation:

#!/usr/bin/env python
# -*- coding:utf-8 -*-


# 定义一个实现加法运算的函数
def test_func(a, b):
    c = a + b
    print(c)

Then, call the function in main as follows:

if __name__ == '__main__':
    a = 2
    b = 3
    test_func(a, b)

output:

5

Guess you like

Origin blog.csdn.net/kevinjin2011/article/details/129405312