Python functions

Functions allow us to create a set of instructions that can be run when needed.

Function is essentialPythonAs well as creating meaningful programs in many other programming languages, because they allow us to decompose programs into manageable parts, thereby improving readability and code reuse.

This is an example function calledhelloPrint "Hello!":

def hello():
    print('Hello!')

This is the functiondefinition. Have a name (hello) And the body (a set of instructions), this is the part following the colon, indented one level to the right on the right.

To run this function, we must call it. This is the syntax for calling the function:

hello()

We can perform this function once or multiple times.

The name of the function,hello, Very important. It should be descriptive, so anyone who calls it can imagine what the function does.

A function can accept one or more parameters:

def hello(name):
    print('Hello ' + name + '!')

In this case, we call the function passing parameters

hello('Roger')

We call itparameterThe values accepted by the function in the function definition, andargumentThe value passed to the function when it is called. It is common to be confused about this distinction.

If the parameter is not specified, the parameter can have a default value:

def hello(name='my friend'):
    print('Hello ' + name + '!')

hello() #Hello my friend!

This is how we accept multiple parameters:

def hello(name, age):
    print('Hello ' + name + ', you are ' + str(age) + ' years old!')

In this case, we call a function passing a set of parameters:

hello('Roger', 8)

Parameters are passed by reference. All types in Python are objects, but some of them are immutable, including integers, booleans, floating-point numbers, strings, and tuples. This means that if they are passed as parameters and their values are modified inside the function, the new values will not be reflected outside the function:

def change(value):
    value = 2

val = 1 change(val)

print(val) #1

If an immutable object is passed and one of its properties is changed, the change will be reflected externally.

Function can be usedreturnstatement. For example, in this case, we returnnameparameter name:

def hello(name):
    print('Hello ' + name + '!')
    return name

When the function is satisfiedreturnStatement, the function ends.

We can ignore this value:

def hello(name):
    print('Hello ' + name + '!')
    return

We can include a return statement inside the condition, which is a common way to end a function when the start condition is not met:

def hello(name):
    if not name:
        return
    print('Hello ' + name + '!')

If the value passed by calling this function is equal toFalse, Like an empty string, the function is arrivingprint()statement.

You can use comma-separated values to return multiple values:

def hello(name):
    print('Hello ' + name + '!')
    return name, 'Roger', 8

under these circumstances,hello('Syd')The return value is a tuple containing these three values:('Syd', 'Roger', 8).


More python tutorials: