Hướng dẫn dùng call use python

Suppose, you need to create a program to create a circle and color it. You can create two functions to solve this problem:

  • create a circle function
  • create a color function

Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.

Types of function

There are two types of function in Python programming:

  • Standard library functions - These are built-in functions in Python that are available to use.
  • User-defined functions - We can create our own functions based on our requirements.

Python Function Declaration

The syntax to declare a function is:

def function_name[arguments]:
    # function body 

    return

Here,

  • def greet[]:
        print['Hello World!']
    9 - keyword used to declare a function
  • def greet[]:
        print['Hello World!']
    0 - any name given to the function
  • def greet[]:
        print['Hello World!']
    1 - any value passed to function
  • def greet[]:
        print['Hello World!']
    2 [optional] - returns value from a function

Let's see an example,

def greet[]:
    print['Hello World!']

Here, we have created a function named

def greet[]:
    print['Hello World!']
3. It simply prints the text
def greet[]:
    print['Hello World!']
4.

This function doesn't have any arguments and doesn't return any values. We will learn about arguments and return statements later in this tutorial.

Calling a Function in Python

In the above example, we have declared a function named

def greet[]:
    print['Hello World!']
3.

def greet[]:
    print['Hello World!']

Now, to use this function, we need to call it.

Here's how we can call the

def greet[]:
    print['Hello World!']
3 function in Python.

# call the function
greet[]

Example: Python Function

def greet[]:
    print['Hello World!']

# call the function
greet[]

print['Outside function']

Output

Hello World!
Outside function

In the above example, we have created a function named

def greet[]:
    print['Hello World!']
3. Here's how the program works:

{IMAGE}

Here,

  • When the function is called, the control of the program goes to the function definition.
  • All codes inside the function are executed.
  • The control of the program jumps to the next statement after the function call.

Python Function Arguments

As mentioned earlier, a function can also have arguments. A arguments is a value that is accepted by a function. For example,

# function with two arguments
def add_numbers[num1, num2]:
    sum = num1 + num2
    print['Sum: ',sum]

# function with no argument
def add_numbers[]:
    # code

If we create a function with arguments, we need to pass the corresponding values while calling them. For example,

# function call with two values
add_numbers[5, 4]

# function call with no value
add_numbers[]

Here,

def greet[]:
    print['Hello World!']
8 specifies that arguments
def greet[]:
    print['Hello World!']
9 and
# call the function
greet[]
0 will get values 5 and 4 respectively.

Example 1: Python Function Arguments

# function with two arguments
def add_numbers[num1, num2]:
    sum = num1 + num2
    print["Sum: ",sum]

# function call with two values
add_numbers[5, 4]

# Output: Sum: 9

In the above example, we have created a function named

# call the function
greet[]
1 with arguments: num1 and num2.

{Working of function with arguments}

We can also call the function by mentioning the argument name as:

add_numbers[num1 = 5, num2 = 4]

In Python, we call it Keyword Argument [or named argument]. The code above is equivalent to

def greet[]:
    print['Hello World!']
0

The return Statement in Python

A Python function may or may not return a value. If we want our function to return some value to a function call, we use the

def greet[]:
    print['Hello World!']
2 statement. For example,

def greet[]:
    print['Hello World!']
1

Here, we are returning the variable

# call the function
greet[]
3 to the function call.

Note: The

def greet[]:
    print['Hello World!']
2 statement also denotes that the function has ended. Any code after return is not executed.

Example 2: Function return Type

def greet[]:
    print['Hello World!']
2

In the above example, we have created a function named

# call the function
greet[]
5. The function accepts a number and returns the square of the number.

{IMAGE: WORKING OF FUNCTION WITH return Values}

Example 3: Add Two Numbers

def greet[]:
    print['Hello World!']
3

Python Library Functions

In Python, standard library functions are the built-in functions that can be used directly in our program. For example,

  • # call the function
    greet[]
    6 - prints the string inside the quotation marks
  • # call the function
    greet[]
    7 - returns the square root of a number
  • # call the function
    greet[]
    8 - returns the power of a number

These library functions are defined inside the module. And, to use them we must include the module inside our program.

For example,

# call the function
greet[]
7 is defined inside the
def greet[]:
    print['Hello World!']

# call the function
greet[]

print['Outside function']
0 module.

Example 4: Python Library Function

def greet[]:
    print['Hello World!']
4

Output

def greet[]:
    print['Hello World!']
5

In the above example, we have used

  • def greet[]:
        print['Hello World!']
    
    # call the function
    greet[]
    
    print['Outside function']
    1 - to compute the square root of 4
  • def greet[]:
        print['Hello World!']
    
    # call the function
    greet[]
    
    print['Outside function']
    2 - computes the power of a number i.e. 23

Here, notice the statement,

def greet[]:
    print['Hello World!']
6

Since

# call the function
greet[]
7 is defined inside the
def greet[]:
    print['Hello World!']

# call the function
greet[]

print['Outside function']
0 module, we need to include it in our program.

Benefits of Using Functions

1. Code Reusable - We can use the same function multiple times in our program which makes our code reusable. For example,

def greet[]:
    print['Hello World!']
7

Output

def greet[]:
    print['Hello World!']
8

In the above example, we have created the function named

def greet[]:
    print['Hello World!']

# call the function
greet[]

print['Outside function']
5 to calculate the square of a number. Here, the function is used to calculate the square of numbers from 1 to 3.

Hence, the same method is used again and again.

2. Code Readability - Functions help us break our code into chunks to make our program readable and easy to understand.

Chủ Đề