GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Python/python6/1.md at master · mingyun/Python

Expand file tree

/

1.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
42 lines (31 loc) · 1.12 KB
/

1.md

Copy path

File metadata and controls

42 lines (31 loc) · 1.12 KB
Raw
Copy raw file
Download raw file
Edit and raw actions

一、Python 自定义函数的基本步骤

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。

自定义函数,基本有以下规则步骤:

  • 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()
  • 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数
  • 函数的第一行语句可以选择性地使用文档字符串(用于存放函数说明)
  • 函数内容以冒号起始,并且缩进
  • return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的 return 相当于返回 None。

语法示例:

def functionname( parameters ):
   "函数_文档字符串"
   function_suite
   return [expression]

实例:

  1. def 定义一个函数,给定一个函数名 sum
  2. 声明两个参数 num1 和 num2
  3. 函数的第一行语句进行函数说明:两数之和
  4. 最终 return 语句结束函数,并返回两数之和
def sum(num1,num2):
	"两数之和"
	return num1+num2

# 调用函数
print(sum(5,6))

输出结果:

11
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.