function (Python)

Source: Internet
Author: User

What is a function?

The function of a computer is a fixed program segment, or it is called a subroutine, it can realize the fixed operation function, but also with an entrance and an exit, so-called entrance, is the function of each parameter, we can through this entrance, the function parameter value into the subroutine, for the computer processing The so-called exit refers to the function value of the function, after the computer is obtained, the port is brought back to the program that called it.

Features of the use of functions: Reduce code duplication, programs can be extended, easy to maintain.

Syntax Definitions:

1 #@Software: Pycharm2 defSay ():#Defining Functions3     Print('Hello World')4 5Say ()#Call function say6 7 8 9 Hello WorldTen  OneProcess finished with exit code 0
View Code

# @Software: PycharmA, B = 5,2def  Calc (x    , y):= x**y    return    = Calc (A, b)print(c)Process finished with exit code 0

Parameters of the function:

Parameter: A variable is allocated only when it is called, and the memory unit is freed when the call ends, that is, it is valid only within the function.

Arguments: You must have a definite value to pass the value to the parameter.

# @Software: PycharmA, B = 5,2#A,real parameter def Calc (x, y):#x, y as parameter    res = x**y    return  = Calc (A, b)# argument passed to formal parameter print  (c)Process finished with exit code 0
View Code

Positional parameters: Arguments are passed when the function is called based on the parameter position defined by the function.

defStudents (Name,age,sex,num):#name age sex num is positional parameter    Print('Here are the student information')    Print('Name:', name)Print('Age:', age)Print('Sex', Sex)Print('School Number:', num) students ('Jack', 22,' Boy', 150313) Students ('Siri', 5,'Girl', 180254Below is the student information name: Jack Age:22Gender Boy study Number:150313Here is the student information name: Siri Age:5Gender girl Number:180254Process finished with exit code 0

Default parameter: The default parameter refers to a value that is automatically used when an argument is omitted from a function call. For example, if you set void Wow (int n) to n with a default value of 1, the function call Wow () is equivalent to Wow (1). This greatly improves the flexibility of using functions.

defStudents (name,age,sex,num,addr='0370'):#addr as default parameter    Print('Here are the student information')    Print('Name:', name)Print('Age:', age)Print('Sex', Sex)Print('School Number:', num)Print('Learn', addr) students ('Jack', 22,' Boy', 150313) Students ('Siri', 5,'Girl', 180254Below is the student information name: Jack Age:22Gender Boy study Number:150313Address0370Here is the student information name: Siri Age:5Gender girl Number:180254Address0370Process finished with exit code 0

Non-fixed parameters: You are not sure how many parameters you need to pass in the definition.

#@File: jia.pydefStudents (Name,age,*args):#*args to put extra values in a single tuple    Print(Name,age,args) students ('Jack', 22) Students ('Siri', 1,'Girl','Read') D:\untitled\venv\Scripts\python.exe D:/untitled/bogls/Jia.pyjack22() Siri1 ('Girl','Read') Process finished with exit code 0

#@File: jia.pydefStudents (Name,age,*args,**kwargs):#**kwargs put extra values in the dictionary    Print(Name,age,args,kwargs) students ('Jack', 22) Students ('Siri', 1,'Girl','Read', sex='Girl', addr='0370') Jack22() {}siri1 ('Girl','Read') {'Sex':'Girl','Addr':'0370'}process finished with exit code 0

function (Python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.