Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Python function (1) What is a function?
Python function (1) What is a function?
Friends who have been exposed to C language must be very familiar with word function. No matter what kind of programming language, functions (of course, in some languages, they are called methods, which have the same meaning) play a vital role. Today, let's take a look at the function usage in Python.

I. Functional definition

In some programming languages, function declaration and function definition are separated (in these programming languages, function declaration and function definition can appear in different files, such as C language), but in Python, function declaration and function definition are regarded as one. In Python, the basic form of function definition is as follows:

Define functions (parameters):

block

Return expression/value explains several points here:

(1) In Python, the def keyword is used to define a function without specifying the type of the return value.

(2) Function parameters params can be zero, one or more. Similarly, function parameters do not need to specify parameter types, because variables are weakly typed in Python, and Python will automatically maintain their types according to their values.

(3) 3) The return statement is optional and can appear anywhere in the function body, indicating the end of the function call execution; If there is no return statement, it will automatically return NONE, and if there is a return statement, but there is no expression or value after return, it will also return NONE.

Let's look at two examples:

def printHello():

Print "Hello"

def printNum():

For I(0, 10) in the range:

Print I

return

def add(a,b):

Return a+b

Print printHello ()

print printNum()

Print addition (1, 2)