Python Learning notes 5-python functions

Source: Internet
Author: User

Function 

The term function is derived from mathematics, but the concept of "function" in programming is very different from the function in mathematics, and the function in programming has many different names in English. In basic it is called subroutine (sub-process or subroutine), in Pascal is called procedure (process) and function, in C only function, in Java is called method.

Defined

A function is to encapsulate a set of statements by a name (function name), and to execute the function, simply call the name of its functions.

Characteristics



3. Make the program easier to maintain

Grammar

def Sayhi ()# use def to define a function followed by the function name ().     print ("hello")# action contents in function, printing a Hellosayhi () # function name + parentheses to implement function invocation # Print Result: Hello

Parameters of the function

# define variable a # define variable B def Calc (x, y)#x , y parameter    res x**    yreturn  res  print (A, b)#A/b argument # printing result: 1024x768

Formal parameter: The memory unit is allocated only when it is called, and the allocated memory unit is freed immediately after the call ends. Therefore, the formal parameter is only valid inside the function. The parameter variable can no longer be used after the function call finishes returning the keynote function.

Note: 1, the parameter is a local variable, scope only inside the function, the external variable by default cannot be modified inside the function, can only be referenced.

2, if you need to modify the global variables inside the function need to use "global", but strongly do not recommend this use.

3, inside the function can modify the list, dictionary, collection, instance.

Arguments: Can be constants, variables, expressions, functions, and so on, regardless of the type of arguments, they must have a definite value when making a function call to pass these values to the parameter. Therefore, the parameters should be pre-assigned, input and so on to obtain a definite value.

Note: There are parameters that determine the value, and all data types can be passed as arguments to the function.

Cond..........

Python Learning notes 5-python functions

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.