Parameters of the python--function

Source: Internet
Author: User

The parameters of the function are divided into:

1. Required Parameters

2. Keyword parameters

3. Default parameters

Required Parameters

The most basic feature is that the parameters that we define in the function's argument list must be passed.

def Add (x, y):    result = x +y    return result

When we call this function, we have to assign values to the X and Y tables, and if two are not assigned or give only one assignment, it will be an error.

When we define this function, we give x y in this parameter list, and we can call X and y called formal parameters, or formal arguments.

There are formal parameters, corresponding to the actual parameters, referred to as real arguments.

The actual parameter refers to the actual value that we pass to the function parameter list during the call to the function.

Keyword parameters

Let's take a function to give an example.

def Add (x, y):    result = x +y    return result

The parameter must be in one order at the time of invocation, the first passed argument is the value of X, and the second is the value of Y

So what if we're going to first pass in the Y value, and the second one is the x value?

The keyword parameter can be used to specify the order of the parameters without regard to the order of the function parameters, so that the parameters can be called.

c = Add (y = 5,x = 2)

So you can assign Y to the value first. Of course, if a parameter is assigned or not, how many parameters are there, and how many arguments will be.

Default parameters

If we need a long parameter list for the function, and we need to use this function more than once, there are a lot of arguments that will be repeated, then we can set a default value for the function.

def print_student_files (name,gender,age):    Print (' I'm called ' + name ')    Print (' I'm old this year   ')    Print (' i  am ' born ')

This is a function that records the student's name you, age, and gender (note that the third line is converted to a string because the string cannot be added to the int type)

>>> print_student_files (' Anne ',' woman ', ') My name is Annie, I'm 18 years old, I'm a girl .

If there are 30 students, most of them are 18-year-old girls. Then we can use the default parameters:

defPrint_student_files (name,gender='female', age= -):    Print('My name'+name)    Print('me this year'+ STR (age) +'years')    Print('I was'+ Gender +'Health')

So, we just need to enter a name.

>>> print_student_files (' mani ') My name is Mani, I'm 18 years old , I'm a girl.

If you have a classmate with a different age and gender and set the default value, you need to pass the corresponding parameter:

>>> print_student_files (' John ',' male ', My name is John, I'm 19 years old, I'm a boy .

Summarize the rules for default parameters:

At the time of definition, if you want a parameter to have a default value, then add a ' = ' to the argument and assign the default value to the formal argument.

If you do not set a default value for a parameter, you must pass it to an argument.

Easy Error Point:

The parameter must be appended with the default parameter, followed by a mandatory parameter

Error instance 1:

defPrint_student_files (name,gender='female', age):Print('My name'+name)Print('me this year'+ STR (age) +'years')    Print('I was'+ Gender +'Health')
syntaxerror:non-default argument follows default argument

Error: Cannot put non-default parameters after default parameters

Error Instance 2:

We want to change the second default parameter (age), the first default parameter (gender invariant)

Let's say we convert each parameter to a string.

>>>defPrint_student_files (name,gender='female', age=18):    Print('My name'+STR (name))Print('me this year'+ STR (age) +'years')    Print('I was'+ STR (gender) +'Health')>>> Print_student_files ('Anne', 17My name is Annie, I'm 18 years old, I'm 17 .

Workaround:

We introduced age 17 as sex into gender parameters. The solution is to use key parameters

>>> print_student_files (' Anne ',age = +) My name is Annie, I'm 17 years old , I'm a girl.

Error Instance 3:

To mix the default parameters with the required parameters is called:

>>> print_student_files (' Anne ', gender=' female ', +) syntaxerror:positional argument follows keyword argument 

Parameters of the python--function

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.