Python create and call functions

Today we are concerned with the creation of Python in the calling function.

Talk about creating a function, create function uses def keyword, define the abbreviation is.

Look at the code on it:

def say(info):
	print(info)

This is a function named say, let's look at the structure of this function:

def function name (parameter list):
function body

Note, def function also needs to create the indentation. Now we talk about passing parameters.

Function parameters can be specified when defining a default value, when a function is called, if no incoming parameter value corresponding to the default value when the function is defined instead.
Optional parameters are generally placed behind the non-optional parameters, i.e., defining a function, given to all non-optional parameters, then the default values for each parameter and the corresponding optional respectively.
Python language supports function parameters in the parameter name passed way.

def function name (non-optional list of parameters, optional parameters = default):
function body

That info is just an example of a non-optional parameters. Non-optional parameters must be given a value at the time of the call, otherwise it will error. We give you an example:

choice = input('选择1还是2?')
def game(choice,win = True):
	if choice == '1':
		win = False
		print('你没赢')
	if choice == '2':
		win = True
		print('你赢了')

Execute it, nothing happened, right. Because the function is not called when the block will not be executed. So we have to call. Python calls the function is very simple, like this:

def say(info):
	print(info)
say('fd')

You know how to call it. In fact, that is:
function name (parameter)

Here we talked about two concepts, the above function is called the info parameter; this 'fd' called arguments when calling. The transfer function is to pass values ​​to the parameter argument. Generally one-transmitted, like this:

def say(info,count):
	for i in range(int(count)):
		print(info)
say('我很好',5)

The fun of it, we now look at the other two types of parameters:

def a(*d):
    print(sum(d))
a(3,2,54)

This parameter is a variable type, that is a plurality of values ​​can be assigned to it. We can look at its type:

def a(*d):
	print(type(d))
a(3,2,54)

Is a tuple, right?

Let us look at one of:

Keyword parameter, which is the print function parameters in the end, end is called keyword arguments, we create a function to look at:

def end_(**b):
    print(b)
end_(end = 123)

Then this, then it is a dictionary type, we should see the results of printing.

Now let me introduce a keyword: return.

If you need to return a value, using the reserved word return and return a list of values. Functions can not return statement, returning control to the caller after the function body.

We give an example:

def fun():
	a = 5
	return a
fun()
print(fun())

See it, return function return values, did not react when called directly. We do not look at the results returned:

def fun():
	a = 5
	print(a)
fun()
print(fun())

The second is not None?

We revisit a very important concept, that is, global variables and local variables.

This is a local variable, such as:

def fun():
	a = 45

This is a local variable, a function call when finished, the program will be freed of all local variables in function, so that local variables within the function can be used outside.

def fun():
	a = 45
print(a)

Error it!

Global variables are those variables that we've learned, called global variables. Global variables can be used throughout the program.

If you want to use that global variables in a function, it is necessary to declare a global keyword, like this:

a = 45
def fun():
	global a
	a = 56
fun()
print(a)

The value of a changed it.

In some cases, global variables used without declaration to the function, for example:

a = [1,5]
def fun():
	a.append(5)
print(a)

Is not add up? This particular case I lack experience, so we try more on the line.

General exam will be out of the question like this to confuse the people.

a = 45
def fun():
	a = 42
print(a)

There are more difficult than this, but I'm a little forgotten ...

Well today is more than knowledge, I hope you remember that this is the basis of this foundation. Writing is not easy, we at least look at it. There are puzzled friend asked in the comments section, I'll try and answer. Partners interested in small, can add my QQ: 3418772261 . Our next goodbye!

Published 13 original articles · won praise 35 · views 10000 +

Guess you like

Origin blog.csdn.net/Persia_king/article/details/105093358