The basic function of using Python

In programming, no matter what programming language, using functions are very wide, the function can perform specific functions, reducing the difficulty of programming and code reuse.

1. Define the function:

Function is a piece with a specific function, reusable group of statements, with the function name to represent and be called by the function name.

2. The functions written thoughts:

Function is a function of abstraction, it is a complex using a large problem can be decomposed into a series of simple small problem, and then continue to a small problem into smaller problems, when a problem is simple enough to refine, for each small problems programming, and by function package, each small problem to solve big problems can be solved.

3.Python function

Python Package also comes with some functions and methods, including Python built-in functions (e.g., abs (), eval ()), Python standard library function (e.g., math library sqrt ()) and the like, which functions in the programming process in all it can be called directly. But in everyday coding process, we also need the actual situation, write custom functions.

4.Python custom function

Python def using reserved words defined function syntax is as follows:

DEF <function name> (parameter list):

  <Function> The

  return <Return value list>

Note: The list of parameters passed to his value when you call the function, you can have zero, one, or more, when passing multiple parameters, separated by commas between the parameters, when no parameters have reserved parentheses.

When you need to return a value, using the reserved word return and return a list of values, otherwise you can not return statement.

The formal parameters of the function

5.1 programming, comparing two integers, the size of three integers, the maximum output. Wherein the code analysis are explained on the parameter and the parameter of the function. <When comparing the size of the direct use of built-in functions max () can be implemented faster>

Source:

Compare the size of three parameters #, num, num1, num2 formal parameters
DEF Number (NUM, num1, num2):
  IF (NUM <num1):
    max = num1
    IF (max <num2):
      max = num2
      Print ( "number three comparing the maximum number is {} "the format (max)).
    the else:
      Print (" three digital comparing the maximum number is {} "the format (max.))
  the else:
    max = NUM
    IF (max <num2):
      max = num2
      Print ( "The maximum number is three digital comparator} {" the format (max)).
    the else:
      Print ( "maximum number is three digital comparator {}" format (max.) )

Argument is a # 1,2,3 function, the argument passed to the parameter values, execute the function body statements
Number (2,3)
# size comparing two parameters, num, num1 formal parameters
def number (num , num1):
  IF (NUM <num1):
    max = num1
    Print ( "two numbers comparing the maximum number of {}" the format (max.))
  the else:
    max = NUM
    Print ( "two numbers comparing the maximum number is {}." format (max))

# 1 and 2 are a function of the argument, the argument passed to the parameter values, execute the function body statements
Number (1,2)

operation result:

 

6. calls the process function

During the function call is generally four steps:

(1) calls the program be suspended at the call.

(2) copy the arguments to the function parameter when invoked.

(3) perform the function body statements.

(4) the end of the function call returns the value given. Callback call to pause before continuing execution.

As in the following example:

Source:

DEF Number (NUM, num1):
  IF (NUM <num1):
    max = num1
    Print ( "two numbers comparing the maximum number is {}" the format (max).)
  the else:
    max = NUM
    Print ( "two numbers comparing the maximum number is {} ". format (max) )

print ( "function has not been called!")
# function call this function before the body has been compiled, it is useless to use, after the function call, the value of the parameter is
# replaced argument function executed on the front and back body statement after the function body statement is finished, go back to the function call
# position, continue to implement the statement following
number (1,2)

print ( "execute this line!")

operation result:

 

Guess you like

Origin www.cnblogs.com/chenting123456789/p/11560289.html