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

📄 ch07s02.html

📁 简明python,一本很不错的书
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>简明 Python 教程 / 函数 / 函数形参 </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">简明 Python 教程</span></th></tr><th colspan="3" align="center">第7章 函数</th><tr><th width="20%" align="left"><a href="ch07.html#s01">上一页</a></th><th width="60%" align="center"><span class="header2">函数形参</span></th><th align="right"><a href="ch07s03.html">下一页</a></th></tr></table><hr noshade><h1>函数形参</h1><p>函数取得的参数是你提供给函数的值,这样函数就可以利用这些值 <dfn>做</dfn> 一些事情。这些参数就像变量一样,只不过它们的值是在我们调用函数的时候定义的,而非在函数本身内赋值。</p><p>参数在函数定义的圆括号对内指定,用逗号分割。当我们调用函数的时候,我们以同样的方式提供值。注意我们使用过的术语——函数中的参数名称为 <dfn>形参</dfn> 而你提供给函数调用的值称为 <dfn>实参</dfn> 。</p><h2><a name="using">使用函数形参</a></h2><p class="exampletitle"><a name="e72">例7.2 使用函数形参</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: func_param.py</code><br><br><code class="key">def </code><code class="func">printMax</code><code>(a, b):</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;if </code><code>a > b:</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </code><code>a, </code><code class="cite">'is maximum'</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;else</code><code>:</code><br><code class="key">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print </code><code>b, </code><code class="cite">'is maximum'</code><br><br><code>printMax(</code><code class="cite">3</code><code>, </code><code class="cite">4</code><code>) </code><code class="comment"># directly give literal values</code><br><br><code>x = </code><code class="cite">5</code><br><code>y = </code><code class="cite">7</code><br><br><code>printMax(x, y) </code><code class="comment"># give variables as arguments</code></p><p>(源文件:<a href="code/func_param.py">code/func_param.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python func_param.py<br>4 is maximum<br>7 is maximum</code></p><h2>它如何工作</h2><p>这里,我们定义了一个称为<code>printMax</code>的函数,这个函数需要两个形参,叫做<code>a</code>和<code>b</code>。我们使用<code>if..else</code>语句找出两者之中较大的一个数,并且打印较大的那个数。</p><p>在第一个<code>printMax</code>使用中,我们直接把数,即实参,提供给函数。在第二个使用中,我们使用变量调用函数。<code>printMax(x, y)</code>使实参<code>x</code>的值赋给形参<code>a</code>,实参<code>y</code>的值赋给形参<code>b</code>。在两次调用中,printMax函数的工作完全相同。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="http://www.byteofpython.info/language/chinese/ch07.htmls01">上一页</a></th><th width="60%" align="center"><a href="ch07.html">上一级</a></th><th width="20%" align="right"><a href="ch07s03.html">下一页</a></th></tr><tr><th width="20%" align="left">简介</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">局部变量</th></tr></table></body></html>

⌨️ 快捷键说明

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