Introduction to custom functions in basic python tutorials

  • 2020-04-02 13:59:21
  • OfStack

The most important purpose of a function is to make it easy for us to reuse the same program.

Attach some operations to a function, and when you want to perform the same operation later, just call the function name instead of repeating all the statements.

Definition of function

First, we need to define a function that illustrates what this function does.


def square_sum(a,b):
    c = a**2 + b**2
    return c

What this function does is find the sum of the squares of two Numbers.

First, def, this keyword tells python: I'm defining a function. Square_sum is the name of the function.

A and b in parentheses are the arguments to the function, the input to the function. Arguments can be multiple or none at all (but the parentheses are reserved).

We have already seen the colons and indentation in the loop and selection.


c = a**2 + b**2        # This is the inside of the function return c               # return c Is the function of the output. Python Is allowed to return no value return . return Multiple values can be returned, separated by commas. It's like returning a tuple( Set the table ) . return a,b,c          # The equivalent of return (a,b,c)

In Python, when the program executes return, the program stops executing the remaining statements in the function. Return is not required. When there is no return, or there is no return value after return, the function will automatically return None. None is a special data type in Python used to indicate that nothing exists, equivalent to NULL in C. None is often used as the default value for keyword parameter passing.

Function calls and parameter passing

Once the function is defined, it can be used in later programs


print square_sum(3,4)

Python knows by location that 3 corresponds to the first argument a in the function definition, and 4 to the second argument b, and then passes the argument to the function square_sum.

(Python has a wealth of ways to pass parameters, as well as keyword, table, dictionary, and so on. The basic tutorial will only cover location passing.)

The function returns 25, which is printed by print.

Let's look at two more examples


a = 1 def change_integer(a):
    a = a + 1
    return a print change_integer(a)
print a #===(Python In the "#" This is followed by comments that are not executed ) b = [1,2,3] def change_list(b):
    b[0] = b[0] + 1
    return b print change_list(b)
print b

In the first example, we pass an integer variable to a function, and the function operates on it, but the original integer variable a does not change.

In the second example, we pass a table to a function, the function operates, and the original table b changes.

For variables of the basic data type, after the variable is passed to the function, the function copies a new variable in memory without affecting the original variable. (we call this value passing)

But for the table, the table is passed to the function is a pointer, pointer to the sequence in memory, in the function of the operation on the table will be in the original memory, thus affecting the original variables. (we call this pointer passing)

conclusion


def function_name(a,b,c):
    statement
    return something  # return It's not necessary

The purpose of the function is to improve the repeatability of the program.

return     None

Pass the parameter through the position.

Parameter of basic data type: value passing

Table as parameter: pointer passed

Practice:

Write a function that determines a leap year with the parameters year, month, and day. Returns True in a leap year


Related articles: