程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Function creation and call in Python

編輯:Python

Catalog

One 、 Preface

Two 、 Create a function

3、 ... and 、 Call function

Four 、pass Empty statement

One 、 Preface

Mention functions , You can think of mathematical functions , Function is the most important module in Mathematics , Throughout the whole mathematics learning , stay Python in , Functions are widely used . We've touched on functions many times before . for example , For output print() function 、 For input input() function , And... For generating a series of integers range() function . These are all Python Built in standard functions , You can use it directly . In addition to the standard functions that can be used directly ,Python Custom functions are also supported . That is, by changing a regular 、 Duplicate code defined as a function , To achieve the purpose of writing multiple calls at one time . Using functions can improve code reuse .

Two 、 Create a function

Creating a function is also called defining a function , It can be understood as creating a tool with a certain purpose .

Use def Keyword implementation , The specific syntax format is as follows :

def functionname([parameterlist]):    ["""comments"""]    [functionbody]

Parameter description :

functionname: The name of the function , Use when calling a function

parameterlist: Optional parameters , Used to specify parameters passed to a function , If you have more than one parameter , Use commas between parameters “,” Separate ; If you don't specify , It means that the function has no parameters . In the call , No parameters are specified .

comments: Optional parameters , Specifying comments for functions , Comments usually describe the function of the function 、 The function of parameters to be passed, etc .

example : Define one according to height 、 Weight calculation BMI Exponential function fun_bmi(), This function includes 3 Parameters , Used to specify names respectively 、 Height and weight , According to the formula :BMI= weight /( height * height ) Calculation BMI Index , And output the result ,

The code is as follows :

def fun_bmi(person, height, weight):    """    function : Based on height and weight BMI Index     :param person: full name     :param height: height , Company : rice     :param weight: weight , Company : kg     """    print(person + " The height of :" + str(height) + " rice \t weight :" + str(weight) + " kg ")    bmi = weight / (height * height)  # Used to calculate BMI Index , Formula for “ weight / Square of height ”    print(person + " Of BMI Index is :" + str(bmi))    # Judge whether the figure is reasonable     if bmi < 18.5:        print(" Your weight is too light ")    if 18.5 <= bmi < 24.9:        print(" normal range , Pay attention to keep ")    if 24.9 <= bmi < 29.9:        print(" You are overweight ")    if bmi > 18.5:        print(" obesity ")

Run the above code , Nothing will be displayed , It doesn't throw an exception , because fun_bmi() The function has not yet called .

3、 ... and 、 Call function

Call function, that is, execute function . If the function created is understood as creating a tool with a specific purpose , Calling a function is equivalent to using the tool . The basic syntax format for calling a function is as follows :

functionname([parametersvalue])

Parameter description :

functionname: The name of the function , Use when calling a function

parametersvalue: Optional parameters , Values used to specify individual parameters . If more than one parameter value needs to be passed , Then a comma is used between the two parameter values “,” Separate ; If the function has no arguments , Write a pair of parentheses directly .

example : Call the above to create fum_bmi function , You can use the following code :

fun_bmi(" anonymous ", 1.76, 50)

Running results , As shown below :

Four 、pass Empty statement

stay Python There is one of them. pass sentence , Represents an empty statement , It doesn't do anything , It usually acts as a placeholder . for example , Create a function , But we don't know what the function is going to do for the time being , It can be used at this time pass Statement fills the body of the function , Express “ I'll fill it in later ”,

The sample code is as follows :

def func():    # pass   # Place holder , Not doing anything

This is about Python This is the end of the article on creating and calling functions in , More about Python Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved