Introduction to custom functions in basic python tutorials

Source: Internet
Author: User
This article mainly introduces the basic python tutorials to introduce user-defined functions. This article describes the definition methods of functions and function parameters in python, the most important purpose of a function is to reuse the same program.

Some operations are affiliated to a function. When you want to implement the same operation in the future, you only need to call the function name, without repeating all the statements.

Function Definition

First, we need to define a function to describe the function.

The Code is as follows:


Def square_sum (a, B ):
C = a ** 2 + B ** 2
Return c


This function is used to calculate the sum of squares of two numbers.

First, def, this keyword notifies python: I am defining a function. Square_sum is the function name.

A and B in parentheses are the parameters of the function and are the input of the function. There can be multiple or none of the parameters (but the brackets should be retained ).

We have seen the affiliation expressed by colons and indentation in the loop and selection.

The Code is as follows:


C = a ** 2 + B ** 2 # This is an internal operation of the function.

Return c # return the value of c, that is, the output function. Python functions allow no return value, that is, no return.

Return can return multiple values separated by commas. Returns a tuple (value table ).

Return a, B, c # is equivalent to 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. If 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 nothing, equivalent to NULL in C. None is mostly the default value used for passing keyword parameters.

Function call and parameter transfer

After defining a function, you can use this function in subsequent programs.

The Code is as follows:


Print square_sum (3, 4)


Python knows through location that 3 corresponds to the first parameter a and 4 in the function definition, corresponding to the second parameter B, and then passes the parameter to the function square_sum.

(Python has rich parameter passing methods, including keyword passing, table passing, and dictionary passing. The basic tutorial will only involve location passing)

After calculation, the function returns value 25, which is printed by print.

Let's look at the following two examples.

The Code is as follows:


A = 1

Def change_integer ():
A = a + 1
Return

Print change_integer ()
Print

#=== (In Python, "#" is followed by a comment without execution)

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 the function. The function operates on it, but the original integer variable a does not change.

In the second example, a table is passed to the function for operations. The original table B changes.

For a variable of the basic data type, after the variable is passed to the function, the function will copy a new variable in the memory, so as not to affect the original variable. (We call this value transfer)

However, for a table, the table is passed to the function as a pointer pointing to the position of the sequence in the memory. operations on the table in the function will be performed in the original memory, thus affecting the original variables. (We call this pointer transfer)

Summary

The Code is as follows:


Def function_name (a, B, c ):
Statement
Return something # return is not required


Function purpose: to improve the repeated availability of the program.

The Code is as follows:


Return None


PASS Parameters by location.

Parameter of the basic data type: Value Transfer

Table as a parameter: pointer Transmission

Exercise:

Write a function for determining the leap year. The parameter is year, month, or day. Returns True if it is a leap year.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.