⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node9.html

📁 Pythone Library reference. it is ok and simple.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by:  Nikos Drakos, CBLU, University of Leeds* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from:  Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Defining Functions</TITLE>
<META content="Defining Functions" name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK 
href="node9_files/easytut.css" rel=STYLESHEET><LINK href="node10.html" 
rel=next><LINK href="node8.html" rel=previous><LINK href="easytut.html" 
rel=up><LINK href="node10.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html" 
name=tex2html259><IMG align=bottom alt=next border=0 height=24 
src="node9_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html255><IMG align=bottom alt=up border=0 height=24 
src="node9_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html" 
name=tex2html249><IMG align=bottom alt=previous border=0 height=24 
src="node9_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html257><IMG align=bottom alt=contents border=0 height=24 
src="node9_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html" 
name=tex2html260>Lists</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html256>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node8.html" 
name=tex2html250>Debugging</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html258>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A 
name=CHILD_LINKS><STRONG>Subsections</STRONG></A> 
<UL>
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html#SECTION00910000000000000000" 
  name=tex2html261>Creating Functions</A> 
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html#SECTION00920000000000000000" 
  name=tex2html262>Variables in functions</A> 
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html#SECTION00930000000000000000" 
  name=tex2html263>Function walkthrough</A> 
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html#SECTION00940000000000000000" 
  name=tex2html264>Examples</A> 
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html#SECTION00950000000000000000" 
  name=tex2html265>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>

<H1><A name=SECTION00900000000000000000>Defining Functions</A> </H1>
<H1><A name=SECTION00910000000000000000>Creating Functions</A> </H1>To start off 
this chapter I am going to give you a example of what you could do but shouldn't 
(so don't type it in): <PRE>a = 23
b = -23

if a &lt; 0:
    a = -a

if b &lt; 0:
    b = -b

if a == b:
    print "The absolute values of", a,"and",b,"are equal"
else:
    print "The absolute values of a and b are different"
</PRE>with the output being: <PRE>The absolute values of 23 and 23 are equal
</PRE>The program seems a little repetitive. (Programmers hate to repeat things 
(That's what computers are for aren't they?)) Fortunately Python allows you to 
create functions to remove duplication. Here's the rewritten example: <PRE>a = 23
b = -23

def my_abs(num):
    if num &lt; 0:
        num = -num
    return num

if my_abs(a) == my_abs(b):
    print "The absolute values of", a,"and",b,"are equal"
else:
    print "The absolute values of a and b are different"
</PRE>with the output being: <PRE>The absolute values of 23 and -23 are equal
</PRE>The key feature of this program is the <TT>def</TT> statement. 
<TT>def</TT> (short for define) starts a function definition. <TT>def</TT> is 
followed by the name of the function <TT>my_abs</TT>. Next comes a <TT>(</TT> 
followed by the parameter <TT>num</TT> (<TT>num</TT> is passed from the program 
into the function when the function is called). The statements after the 
<TT>:</TT> are executed when the function is used. The statements continue until 
either the indented statements end or a <TT>return</TT> is encountered. The 
<TT>return</TT> statement returns a value back to the place where the function 
was called. 
<P>Notice how the values of <TT>a</TT> and <TT>b</TT> are not changed. Functions 
of course can be used to repeat tasks that don't return values. Here's some 
examples: <PRE>def hello():
    print "Hello"

def area(width,height):
    return width*height

def print_welcome(name):
    print "Welcome",name
    
hello()
hello()

print_welcome("Fred")
w = 4
h = 5
print "width =",w,"height =",h,"area =",area(w,h)
</PRE>with output being: <PRE>Hello
Hello
Welcome Fred
width = 4 height = 5 area = 20
</PRE>That example just shows some more stuff that you can do with functions. 
Notice that you can use no arguments or two or more. Notice also when a function 
doesn't need to send back a value, a return is optional. 
<P>
<H1><A name=SECTION00920000000000000000>Variables in functions</A> </H1>
<P>Of course, when eliminiating repeated code, you often have variables in the 
repeated code. These are dealt with in a special way in Python. Up till now, all 
variables we have see are global variables. Functions have a special type of 
variable called local variables. These variables only exist while the function 
is running. When a local variable has the same name as another variable such as 
a global variable, the local variable hides the other variable. Sound confusing? 
Well, hopefully this next example (which is a bit contrived) will clear things 
up. 
<P><PRE>a_var = 10
b_var = 15
e_var = 25

def a_func(a_var):
    print "in a_func a_var = ",a_var
    b_var = 100 + a_var
    d_var = 2*a_var
    print "in a_func b_var = ",b_var
    print "in a_func d_var = ",d_var
    print "in a_func e_var = ",e_var
    return b_var + 10

c_var = a_func(b_var)

print "a_var = ",a_var
print "b_var = ",b_var
print "c_var = ",c_var
print "d_var = ",d_var
</PRE>
<P>The output is: <PRE>in a_func a_var =  15
in a_func b_var =  115
in a_func d_var =  30
in a_func e_var =  25
a_var =  10
b_var =  15
c_var =  125
d_var = 
Traceback (innermost last):
  File "separate.py", line 20, in ?
    print "d_var = ",d_var
NameError: d_var
</PRE>
<P>In this example the variables <TT>a_var</TT>, <TT>b_var</TT>, and 
<TT>d_var</TT> are all local variables when they are inside the function 
<TT>a_func</TT>. After the statement <TT>return b_var + 10</TT> is run, they all 
cease to exist. The variable <TT>a_var</TT> is automatically a local variable 
since it is a parameter name. The variables <TT>b_var</TT> and <TT>d_var</TT> 
are local variables since they appear on the left of an equals sign in the 
function in the statements <CODE>b_var = 100 + a_var</CODE> and <CODE>d_var = 
2*a_var</CODE> . 
<P>As you can see, once the function finishes running, the local variables 
<TT>a_var</TT> and <TT>b_var</TT> that had hidden the global variables of the 
same name are gone. Then the statement <CODE>print "a_var = ",a_var</CODE> 
prints the value <TT>10</TT> rather than the value <TT>15</TT> since the local 
variable that hid the global variable is gone. 
<P>Another thing to notice is the <TT>NameError</TT> that happens at the end. 
This appears since the variable <TT>d_var</TT> no longer exists since 
<TT>a_func</TT> finished. All the local variables are deleted when the function 
exits. If you want to get something from a function, then you will have to use 
<TT>return something</TT>. 
<P>One last thing to notice is that the value of <TT>e_var</TT> remains 
unchanged inside <TT>a_func</TT> since it is not a parameter and it never 
appears on the left of an equals sign inside of the function <TT>a_func</TT>. 
When a global variable is accessed inside a function it is the global variable 
from the outside. 
<P>Functions allow local variables that exist only inside the function and can 
hide other variables that are outside the function. 
<P>
<H1><A name=SECTION00930000000000000000>Function walkthrough</A> </H1>
<P>Now we will do a walk through for the following program: <PRE>def mult(a,b):
    if b == 0:
        return 0
    rest = mult(a,b - 1)
    value = a + rest
    return value

print "3*2 = ",mult(3,2)
</PRE>
<P>Basically this program creates a positive integer multiplication function 
(that is far slower than the built in multiplication function) and then 
demonstrates this function with a use of the function. 
<P><B>Question: </B>What is the first thing the program does? 
<P><B>Answer: </B>The first thing done is the function mult is defined with the 
lines: <PRE>def mult(a,b):
    if b == 0:
        return 0
    rest = mult(a,b - 1)
    value = a + rest
    return value
</PRE>This creates a function that takes two parameters and returns a value when 
it is done. Later this function can be run. 
<P><B>Question: </B>What happens next? 
<P><B>Answer: </B>The next line after the function, <CODE>print "3*2 = 
",mult(3,2)</CODE> is run. 
<P><B>Question: </B>And what does this do? 
<P><B>Answer: </B>It prints <CODE>3*2 = </CODE>and the return value of 
<CODE>mult(3,2)</CODE> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -