python--004--function definition

Source: Internet
Author: User
Tags function definition

···· Function
Consists of a set of code that accomplishes a specific task
Keywords: def
The creation of the function:
def function name (parameter list):
Code (Implementation of the function)
Def printhelloworld ():
For I in range (10):
print ' Hello world '

Call to function:
Function name (parameter)

····· The mechanism of function operation
When calling a function, PVM will find the function's definition position according to the function name, then jump inside the function and execute the internal code of the function once.
EG1:
Def printhelloworld ():
For I in range (10):
print ' Hello world '
Printhelloworld ()

EG2:
def myFunction (s):
For I in range (15):
Print S,
print ' \ n '

Print (' Day depends on mountain ')
MyFunction (' * ')
Print (' Yellow River into the ocean ')
MyFunction (' + ')

Print (' To be poor ')
MyFunction ('-')
Print (' Up to the Heights ')

EG3:
def myFunction (s,n):
For I in range (n):
Print S,
Print

Print (' Day depends on mountain ')
MyFunction (' * ', 10)
Print (' Yellow River into the ocean ')
MyFunction (' + ', 15)

Print (' To be poor ')
MyFunction ('-', 6)
Print (' Up to the Heights ')

··· Where the function is defined
Functions have sequence and scope of action
Functions can also be defined inside another function

Def function1 ():
print ' F1 '
Def function3 ():
print ' F3 '
Function3 ()
Def function2 ():
print ' F2 '
Function1 ()

Can not be called in a random call only inside the function

··· Structure of the function
1. Definition of function (declaration):
def function name (List of functions):
Code (Implementation of the function)
2. Note:
Parameter not defined: no parameter
Multiple parameters: separated by commas
The declaration of a function must have a colon at the end
def add (num1,num2):
result = Num1 + num2
Print result

Add (+)

····· return value of the function

    return携带值    一个函数,如果有return语句,return语句后面的值就是返回值,如果return语句后面没有任何值,返回一个None    一个函数,如果没有return语句,系统会默认在函数末尾添加return,    def fun1():return 1

Def fun2 ():
Pass
A=FUN1 ()
B=fun2 ()
Print A, b

python--004--function definition

Related Article

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.