first, the function
1, the function of the use of the scene and definition
Before the use of function programming, we have been oriented to procedural programming, the accumulation of statements resulting in a large number of code duplication, thus, functions come into being, functional programming is to wrap the code used repeatedly in the function, the use of only a Call. The learning of functions will allow us to understand the abstract concept of object-oriented more quickly, and object-oriented is to classify and encapsulate Functions.
#函数的定义def first_func (): Print ("this is my first Function!") First_func ()
Def: A keyword that defines a function
first_func: function name (custom), call function by function name
(): arguments that can be passed to a function
Print ...: function body, containing function statements for all functions
First_func (): Calling function
The Function's invocation results are as follows
C:\python36\python36.exe D:/cto3jym/day3/def_1.pythis is my first function!
Return value: The data returned to the caller after the function has finished executing
2. Return value of function
The return value of the function is none by default, and when there is a return value in the function body, there is a return value, so we need to know the result of the function can be sent as below example
import smtplibfrom email.mime.text import mimetextfrom email.utils import Formataddrdef mail (): ret = true try: msg = mimetext (' Mail content ', ' plain ', ' utf-8 ') msg[' from '] = formataddr (["jym", ' [email protected] ']) msg[' to '] = formataddr (["gg", ' [email protected] ') msg[' Subject '] = "theme" server = smtplib. SMTP ("smtp.126.com", " server.login", "[email protected]", " ") server.sendmail (' [email protected] ', [' [email protected] ',],msg.as_string ()) server.quit () except exception: ret = false return retret = mail () Print (ret)
As shown in the example, a return value of TRUE indicates that the function executed successfully and the message was sent
3. Parameters
1, no parameters: shape as the above example of sending mail, mail (): no parameter, then in the call () also do not fill in the parameters
2, have parameters
1. General Parameters: Specify several parameters when defining a function, and pass in several parameters when calling
#一个参数def Show (arg): print (arg) show ("one Parameter") #两个参数def show (a1,a2): print (a1,a2) show ("both Parameter")
2. Default parameter: Specifies the default value of the parameter when the function is defined, returns the default value when the call is not passed, and the passed parameter returns the passed parameter
Def show (a1,a2=1): print (a1,a2) show ("default",) return value: C:\Users\JYM\untitled\Scripts\python.exe d:/cto3jym/day3/func_ Parameter.pydefault 1def show (a1,a2=1): print (a1,a2) show ("default", 2) return value: C:\Users\JYM\untitled\Scripts\python.exe D:/cto3jym/day3/func_parameter.pydefault 2
Ps: The default parameter must be behind the position parameter
3. Specify parameters: specify parameters based on the variable name when calling
Def show (a1,a2): print (a1,a2) show (a2=2,a1=1) return value: C:\Users\JYM\untitled\Scripts\python.exe d:/cto3jym/day3/func_ Parameter.py1 2
4. Dynamic Parameters:
This article is from the "linux sailing" blog, make sure to keep this source http://jiayimeng.blog.51cto.com/10604001/1932431
Python Learning Diary Seventh-function