Python自学笔记11-函数的定义和调用

函数是组织代码的一种非常有效的方式,有了函数,我们就可以编写大型项目。可以说,函数是组织代码的最小单位。

[En]

Functions are a very effective way to organize code, and with functions, we can write large-scale projects. It can be said that the function is the smallest unit that organizes the code.

Python函数的定义

函数是代码封装的一种方式,它包含一段可以重复执行的代码。当您需要使用此代码时,只需调用函数并运行函数中的代码即可。

[En]

A function is a means of code encapsulation, which contains a piece of code that can be executed repeatedly. When you need to use this code, you only need to call the function and run the code in the function.

python 函数这么定义:

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;&#x51FD;&#x6570;&#x540D;&#x79F0;<span class="hljs-params">(&#x53C2;&#x6570;<span class="hljs-number">1</span>,&#xA0;&#x53C2;&#x6570;<span class="hljs-number">2</span>)</span>&#xFF1A;<br>&#xA0;&#x51FD;&#x6570;&#x4F53;&#xFF08;&#x8981;&#x8FD0;&#x884C;&#x7684;&#x4EE3;&#x7801;&#xFF09;<br></span>

例如,一个非常简单的函数定义:

[En]

For example, a very simple function definition:

def add(a, b):
    return a + b

现在,让我们给出一个函数功能的例子。

[En]

Now let’s give an example of what a function does.

print('&#x6253;&#x5F00;&#x9AD8;&#x7EA7;&#x8F66;&#x95E8;')
print('&#x5F00;&#x53D1;&#x52A8;&#x673A;')
print('&#x5B89;&#x5168;&#x5E26;')
print('&#x8E29;&#x6CB9;&#x95E8;')
print('&#x638C;&#x63E1;&#x65B9;&#x5411;')

如果你必须多次开车,你每次都需要输入相同的五行代码,这可能会导致几个问题:

[En]

If you have to drive many times, you need to type the same five lines of code each time, which can cause several problems:

  • 很容易编写不同的代码,或者代码出现错误
  • 代码冗余,可读性不强
  • 不容易维护,当需要修改某个步骤时,要修改多处。

Python函数的调用

现在我们可以将上面的代码封装在函数中,当我们需要运行代码时,可以调用该函数,这减少了大量复制粘贴或手动编写多行代码的步骤,函数的名称也表明了代码的作用。当需要修改部分逻辑时,只需修改函数体即可。

[En]

Now we can encapsulate the above code in the function, and when we need to run the code, we can call the function, which reduces a lot of steps of copying and pasting or writing multiple lines of code by hand, and the name of the function also indicates the role of the code. when you need to modify part of the logic, you only need to modify the function body.

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">run_car</span><span class="hljs-params">()</span>:</span><br>&#xA0;&#xA0;&#xA0;&#xA0;print(<span class="hljs-string">'&#x6253;&#x5F00;&#x9AD8;&#x7EA7;&#x8F66;&#x95E8;'</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;print(<span class="hljs-string">'&#x53D1;&#x52A8;&#x53D1;&#x52A8;&#x673A;'</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;print(<span class="hljs-string">'&#x7CFB;&#x4E0A;&#x5B89;&#x5168;&#x5E26;'</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;print(<span class="hljs-string">'&#x8E29;&#x6CB9;&#x95E8;'</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;print(<span class="hljs-string">'&#x638C;&#x63E1;&#x65B9;&#x5411;'</span>)<br><br>run_car()<br><br>run_car()

Python函数的返回值

return 表示当函数执行完之后,想让函数外面的程序得到函数执行的结果, return 的值可以任意的数据类型。

def add(a, b):
    return a + b

result = add(3,4)
print(result)

  • 函数体当中的代码, 遇到 return 会终止运行。
  • 函数的返回结果可以是任意类型,包括 None
  • 当函数体当中没有 return ,默认返回 None

Python函数可以返回多个值, 只需要在多个值之间加上逗号,得到的是元组类型。

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">add</span><span class="hljs-params">(a,&#xA0;b)</span>:</span><br>&#xA0;<span class="hljs-keyword">return</span>&#xA0;a,&#xA0;b

参数的四种类型

return 表示函数返回给外部的值,而参数表示函数从外部接收的值。在函数定义时候的参数叫做形式参数,它其实就是一个变量名称,在函数调用时候的参数叫做实际参数,相当于给变量赋值。

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">get_coffee</span><span class="hljs-params">(suger)</span>:</span><br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">if</span>&#xA0;suger&#xA0;>&#xA0;<span class="hljs-number">100</span>:<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">return</span>&#xA0;<span class="hljs-string">'&#x751C;&#x5496;&#x5561;'</span><br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">elif</span>&#xA0;suger&#xA0;< <span class="hljs-number">1:<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">return</span>&#xA0;<span class="hljs-string">'&#x7F8E;&#x5F0F;'</span><br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">else</span>:<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">return</span>&#xA0;<span class="hljs-string">'&#x83AB;&#x540D;&#x5176;&#x5999;&#x7684;&#x5496;&#x5561;'</span><br><br>result&#xA0;=&#xA0;get_coffee(<span class="hljs-number">100</span>)<br>print(result)<br></ <span>

请注意,形式参数的数量应该与实际参数的数量相同,顺序应该相同,这称为位置参数。(与领导共进晚餐)如果形式参数的数量与实际参数的数量不一致,程序将无法单独赋值:

[En]

Note that the number of formal parameters should be the same as the number of actual parameters, and the order should be the same, which is called position parameters. (having dinner with the leader) if the number of formal parameters is inconsistent with the number of actual parameters, the program will not be able to assign values separately:

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">add</span><span class="hljs-params">(a,&#xA0;b)</span>:</span><br>&#xA0;&#xA0;&#xA0;&#xA0;c&#xA0;=&#xA0;a&#xA0;+&#xA0;b&#xA0;-&#xA0;<span class="hljs-number">1</span><br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">return</span>&#xA0;c<br><br>add(<span class="hljs-number">5</span>,&#xA0;<span class="hljs-number">7</span>,&#xA0;<span class="hljs-number">8</span>)&#xA0;

关键字参数:调用函数时,做关键字标记,防止参数传递错误,造成损失。主要是当参数很多的时候,你记不住顺序和关键字来识别数据的含义。

[En]

Keyword parameter: when calling the function, make a keyword mark to prevent the error of passing parameters and causing loss. Mainly when there are a lot of parameters, you can’t remember the order and keywords to identify the meaning of the data.

默认参数:定义函数时,直接赋值一个变量作为缺省值。就是使用缺省值直接赋值,在调用函数时,可以传递较少的参数。关键字参数或默认参数必须放在位置参数之后。

[En]

Default parameter: when the function is defined, a variable is directly assigned to become the default value. Is to use the default value to assign directly, when calling the function, you can pass fewer parameters. Keyword parameters, or default parameters, must be placed after the position parameter.

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">add</span><span class="hljs-params">(a,&#xA0;b=<span class="hljs-number">0</span>)</span>:</span><br>&#xA0;&#xA0;&#xA0;&#xA0;c&#xA0;=&#xA0;a&#xA0;+&#xA0;b&#xA0;-&#xA0;<span class="hljs-number">1</span><br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">return</span>&#xA0;c<br><br>add(a=<span class="hljs-number">4</span>,&#xA0;b=<span class="hljs-number">6</span>)<br>add(<span class="hljs-number">3</span>)

自动化测试场景:添加多个数据并运行多个用例

[En]

Automated test scenarios: add multiple data and run multiple use cases

<span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">write_case</span><span class="hljs-params">(number)</span>:</span><br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">for</span>&#xA0;_&#xA0;<span class="hljs-keyword">in</span>&#xA0;range(number):<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;username&#xA0;=&#xA0;input(<span class="hljs-string">"&#x8BF7;&#x8F93;&#x5165;&#x7528;&#x6237;&#x540D;:"</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;passwd&#xA0;=&#xA0;input(<span class="hljs-string">"&#x8BF7;&#x8F93;&#x5165;&#x5BC6;&#x7801;:"</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;age&#xA0;=&#xA0;input(<span class="hljs-string">"&#x8BF7;&#x8F93;&#x5165;&#x5E74;&#x9F84;:"</span>)<br><br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;user&#xA0;=&#xA0;dict()<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;user.update(username=username,<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;passwd=passwd,<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;age=age)<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">yield</span>&#xA0;user<br><br><span class="hljs-function"><span class="hljs-keyword">def</span>&#xA0;<span class="hljs-title">run</span><span class="hljs-params">(case)</span>:</span><br>&#xA0;&#xA0;&#xA0;&#xA0;print(<span class="hljs-string">f"&#x8FD0;&#x884C;&#x7528;&#x4F8B;-&#x7528;&#x6237;&#x540D;<span class="hljs-subst">{case[<span class="hljs-string">'username'</span>]}</span>"</span>)<br><br>&#xA0;&#xA0;&#xA0;&#xA0;cases&#xA0;=&#xA0;write_case(<span class="hljs-number">2</span>)<br>&#xA0;&#xA0;&#xA0;&#xA0;<span class="hljs-keyword">for</span>&#xA0;case&#xA0;<span class="hljs-keyword">in</span>&#xA0;cases:<br>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;run(case)

Original: https://www.cnblogs.com/heniu/p/16637408.html
Author: 和牛
Title: Python自学笔记11-函数的定义和调用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/498543/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球