Next we start to learn about "functions." The function can do the following 3 things.
1, they name the code snippet, just like "variable" to string and to name
2, they can accept the parameters, just like your script accepts argv.
3, using # and #, they let you create "mini script" or "small command"
First, put the code: (slightly modified with the code in the textbook)
#-*-coding:utf-8-*-
#this One is like your script with argv
def print_two (*args):
arg1, arg2,arg3 = args
Print "Arg1:%r,arg2:%r,arg3:%r"% (ARG1,ARG2,ARG3)
def print_two_again (Arg1, arg2):
print "arg1:%r,arg2:%r" % (ARG1,ARG2)
def print_several (Arg1, arg2): print ' This is the ' the ' ' is the ' the ' ' is the ' the ' ' is the ' ' is ' arg1
Arg2:%r "%arg2
#this just takes on argument
def print_one (arg1):
print" arg1:%r "%arg1
#this one take No argument
def print_none ():
print "I got Nothing"
print_two ("Zed", "Shaw", "Handsome")
Print_ Two_again ("Zed", "Shaw")
print_one ("first!")
Print_none
Print ("Python", "interesting")
The results of the operation are as follows:
simengred$ python ex18.py
arg1: ' Zed ', arg2: ' Shaw ', Arg3: ' Handsome '
arg1: ' Zed ', arg2: ' Shaw '
arg1: ' first! '
(' Python ', ' interesting ')
The following are the considerations when creating a function:
1, the function begins with Def
2,def is followed by the function name, which is the same as the naming rule for the variable.
3, the function is followed by parentheses ()
4, the parentheses inside the function parameters, multiple parameters separated by commas, and the function name can not be repeated.
5, the parentheses need to write a colon:
6, the function below the content of a indent, the end of the function indentation end