Full-stack Python development: python function basics and python function development Basics

Source: Internet
Author: User

Full-stack Python development: python function basics and python function development Basics
Pytthon function 1. What is a function?

Definition: A function encapsulates a set of statements by a name (function name). to execute this function, you only need to call the function name.

Features:

1. reduce repeated code

2. Easy extension of the program

3. The program becomes easy to maintain

Ii. Function Definition (syntax)

Def: defines the keywords of a function.

Function Name: used to call a function (the definition of a function name must have a descriptive meaning, so that people can probably know what the function is)

Description: function usage instructions.

Code block: code for implementing functions

Return: return Value of the Function

Eg:

Def function name (parameter 1, parameter 2...): ''' document description '''
Code Block 1 code block 2... return value

 

Calculate the sum of two numbers (a, B)

Eg:

def my_sum(a,b):    sum = a+b    return sum
Iii. function call

1. Call Method

Res = function name (real parameter, real parameter)

Find the memory address of the function based on the function name, and then execute the function body code.

Eg: Call the my_sum (a, B) function defined above.

# Calculate the sum of x, y and x = 10y = 20res = my_sum (x, y) print (res)

 

2. rules to be followed

First define and then call principles

Define a function to call it directly, which is equivalent to referencing a non-existent variable (error: xxx is not define)

Def foo (): print ('call function foo () ') bar () def bar (): print ('call function bar ()') # Call function foo ()


Result:
The function foo () is called ()
The function bar () is called ()

 

The following call method violates the principle of first defining and then calling, and an error occurs.

Def foo (): print ('call function foo () ') bar () # Call function foo () def bar (): print (' Call function bar () ')

Error:

NameError: name 'bar' is not defined

 

Iv. return value of a function)

Purpose: exit the function and selectively return the result to the caller (that is, the return value. The default return value is None)

The return value can be in the following three forms:

1. No return, return value None

Def func1 (): print ('the return value of this function is none ')

2. return a value and return the value itself

Def func2 (): print ('the return value of this function is: Hello, ') return 'hello'

3. return multiple values, separated by commas (,). A single tuples are returned.

Def func3 (): print ('the return value of this function is (, 6) ') return 1, 2, 3, 5, 6

 

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.