Hướng dẫn types of arguments in python - geeksforgeeks - các loại đối số trong python - geeksforgeeks

Các hàm Python là một khối các câu lệnh trả về nhiệm vụ cụ thể. is a block of statements that return the specific task.

Show

Ý tưởng là đặt một số tác vụ thường hoặc liên tục được thực hiện lại với nhau và thực hiện một hàm để thay vì viết cùng một mã nhiều lần cho các đầu vào khác nhau, chúng ta có thể thực hiện các cuộc gọi chức năng để sử dụng lại mã có trong đó nhiều lần. & NBSP;

Cú pháp: Hàm Python & NBSP; Python Functions 

Hướng dẫn types of arguments in python - geeksforgeeks - các loại đối số trong python - geeksforgeeks

Tạo chức năng Python

Chúng ta có thể tạo chức năng A & NBSP; Python bằng cách sử dụng từ khóa DEF.def keyword.

Python3

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
5

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
6
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
9
The addition of 5 and 15 results 20.
0

Gọi chức năng A & NBSP; Python

Sau khi tạo một hàm, chúng ta có thể gọi nó bằng cách sử dụng tên của hàm theo sau là dấu ngoặc đơn chứa các tham số của hàm cụ thể đó.

Python3

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
5

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
9
The addition of 5 and 15 results 20.
0

The addition of 5 and 15 results 20.
8

Output:

Welcome to GFG

Gọi chức năng A & NBSP; Python

Sau khi tạo một hàm, chúng ta có thể gọi nó bằng cách sử dụng tên của hàm theo sau là dấu ngoặc đơn chứa các tham số của hàm cụ thể đó.

Xác định và gọi một hàm với các tham sốPython Function with parameters 

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression

Nếu bạn có kinh nghiệm trong C/C ++ hoặc Java thì bạn phải suy nghĩ về loại trả về của hàm và loại dữ liệu của các đối số. Điều đó cũng có thể xảy ra trong Python (cụ thể là cho Python 3.5 trở lên).

Python3

Cú pháp: Hàm Python với tham số & NBSP;

Ví dụ sau đây sử dụng các đối số mà bạn sẽ học sau trong bài viết này để bạn có thể quay lại nó một lần nữa nếu không được hiểu. Nó được định nghĩa ở đây cho những người có kinh nghiệm trước đây bằng các ngôn ngữ như C/C ++ hoặc Java.

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
False True
0
False True
1
False True
2
False True
1
False True
4
False True
5
False True
6
False True
1
False True
8

The addition of 5 and 15 results 20.
3
even
odd
0
even
odd
1
even
odd
2
even
odd
3
even
odd
4

The addition of 5 and 15 results 20.
3
even
odd
6
even
odd
7

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
Geeks Practice
Geeks Practice
7
Geeks Practice
Geeks Practice
8
The addition of 5 and 15 results 20.
0

Output:

The addition of 5 and 15 results 20.

even
odd
8
even
odd
1
Geeks Practice
Geeks Practice
0
Geeks Practice
Geeks Practice
1
Geeks Practice
Geeks Practice
2

Geeks Practice
Geeks Practice
3
even
odd
1
Geeks Practice
Geeks Practice
5
The following examples are defined using syntax 1, try to convert them in syntax 2 for practice.

Python3

Một số ví dụ khác như sau:

Lưu ý: Các ví dụ sau được xác định bằng cú pháp 1, hãy thử chuyển đổi chúng trong cú pháp 2 để thực hành.

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
Hello
Welcome
to
GeeksforGeeks
1

The addition of 5 and 15 results 20.
3
Hello
Welcome
to
GeeksforGeeks
3
Hello
Welcome
to
GeeksforGeeks
4
Hello
Welcome
to
GeeksforGeeks
5
Hello
Welcome
to
GeeksforGeeks
6
Hello
Welcome
to
GeeksforGeeks
7
Geeks Practice
Geeks Practice
1
Hello
Welcome
to
GeeksforGeeks
9
first == Geeks
mid == for
last == Geeks
0

first == Geeks
mid == for
last == Geeks
1
even
odd
6
first == Geeks
mid == for
last == Geeks
3

The addition of 5 and 15 results 20.
3
Hello
Welcome
to
GeeksforGeeks
3
first == Geeks
mid == for
last == Geeks
6
even
odd
1
even
odd
1
first == Geeks
mid == for
last == Geeks
9
False True
4__

first == Geeks
mid == for
last == Geeks
1
even
odd
6
Function to check if the number is even or odd
1

The addition of 5 and 15 results 20.
3
Function to check if the number is even or odd
3
even
odd
1
Hello
Welcome
to
GeeksforGeeks
9

The addition of 5 and 15 results 20.
3
Function to check if the number is even or odd
7
Function to check if the number is even or odd
3
Function to check if the number is even or odd
9
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
00
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
02

first == Geeks
mid == for
last == Geeks
1
Hello
Welcome
to
GeeksforGeeks
3
Hello
Welcome
to
GeeksforGeeks
4
Syntax: print(function_name.__doc__)
3
Function to check if the number is even or odd
3
even
odd
1
even
odd
1

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
12
even
odd
6
Function to check if the number is even or odd
1

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
24
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
25
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
26
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
27
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
28

Output:

False True

first == Geeks mid == for last == Geeks1Function to check if the number is even or odd3even odd3even odd1 Hello Welcome to GeeksforGeeks7

The addition of 5 and 15 results 20.
3
even
odd
6
first == Geeks
mid == for
last == Geeks
3

Đối số của hàm python

Python3

Đối số là các giá trị được truyền bên trong dấu ngoặc đơn của hàm. Một hàm có thể có bất kỳ số lượng đối số được phân tách bằng dấu phẩy.

Trong ví dụ này, chúng tôi sẽ tạo một chức năng đơn giản để kiểm tra xem số được truyền dưới dạng đối số cho hàm là chẵn hoặc lẻ.

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
43
The addition of 5 and 15 results 20.
0

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
46
False True
8

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
51
The addition of 5 and 15 results 20.
0

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
53
Hello
Welcome
to
GeeksforGeeks
7
The addition of 5 and 15 results 20.
0

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
53
Hello
Welcome
to
GeeksforGeeks
9
The addition of 5 and 15 results 20.
0

Output:

even
odd

def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression30

The addition of 5 and 15 results 20.
3
Hello
Welcome
to
GeeksforGeeks
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
33
Syntax: print(function_name.__doc__)
3
Hello
Welcome
to
GeeksforGeeks
7
even
odd
1
even
odd
1

Các loại đối số

Python hỗ trợ nhiều loại đối số khác nhau có thể được thông qua tại thời điểm gọi chức năng. Hãy để thảo luận về từng loại chi tiết.

Python3

Đối số mặc định

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
67
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
68

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
72
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
73

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
74
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
75
The addition of 5 and 15 results 20.
0

Đối số mặc định là một tham số giả định giá trị mặc định nếu giá trị không được cung cấp trong lệnh gọi hàm cho đối số đó. Ví dụ sau minh họa các đối số mặc định. & NBSP;

def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression60even odd1 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression62Syntax: print(function_name.__doc__)8

Giống như các đối số mặc định C ++, bất kỳ số lượng đối số nào trong hàm có thể có giá trị mặc định. Nhưng một khi chúng ta có một đối số mặc định, tất cả các đối số ở bên phải cũng phải có các giá trị mặc định.

Python3

Từ khóa đối số

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
81

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
82
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
84
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
85
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
87
The addition of 5 and 15 results 20.
0

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
89
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
87
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
92
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
84
The addition of 5 and 15 results 20.
0

Ý tưởng là cho phép người gọi chỉ định tên đối số với các giá trị để người gọi không cần nhớ thứ tự của các tham số.

Geeks Practice
Geeks Practice

def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression4 def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression78

Đầu ra

  • Đối số có độ dài thay đổi
  • Trong Python, chúng ta có thể chuyển một số lượng đối số thay đổi cho một hàm bằng các ký hiệu đặc biệt. Có hai biểu tượng đặc biệt:

*args (đối số không thay đổi) Variable length non-keywords argument

Python

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
74
Function to check if the number is even or odd
9
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
99

The addition of 5 and 15 results 20.
3
The addition of 5 and 15 results 20.
01
The addition of 5 and 15 results 20.
022____65
The addition of 5 and 15 results 20.
04

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
The addition of 5 and 15 results 20.
07

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
74
The addition of 5 and 15 results 20.
09
Geeks Practice
Geeks Practice
1
The addition of 5 and 15 results 20.
11
Geeks Practice
Geeks Practice
1
The addition of 5 and 15 results 20.
13
Geeks Practice
Geeks Practice
1
The addition of 5 and 15 results 20.
15
The addition of 5 and 15 results 20.
0

Đầu ra

Hello
Welcome
to
GeeksforGeeks

Ví dụ 2: Đối số từ khóa độ dài thay đổiVariable length keyword arguments

Python3

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
74
Function to check if the number is even or odd
9
Function to check if the number is even or odd
9
The addition of 5 and 15 results 20.
21

The addition of 5 and 15 results 20.
3
The addition of 5 and 15 results 20.
01
The addition of 5 and 15 results 20.
24
Hello
Welcome
to
GeeksforGeeks
5
The addition of 5 and 15 results 20.
26

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
The addition of 5 and 15 results 20.
30
Syntax: print(function_name.__doc__)
3
The addition of 5 and 15 results 20.
32

The addition of 5 and 15 results 20.
33
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
84
The addition of 5 and 15 results 20.
36
even
odd
1
The addition of 5 and 15 results 20.
38
The addition of 5 and 15 results 20.
39
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
84
The addition of 5 and 15 results 20.
0

Đầu ra

first == Geeks
mid == for
last == Geeks

Ví dụ 2: Đối số từ khóa độ dài thay đổi

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
74
Function to check if the number is even or odd
9
Function to check if the number is even or odd
9
The addition of 5 and 15 results 20.
21

The addition of 5 and 15 results 20.
3
The addition of 5 and 15 results 20.
01
The addition of 5 and 15 results 20.
24
Hello
Welcome
to
GeeksforGeeks
5
The addition of 5 and 15 results 20.
26

Syntax: print(function_name.__doc__)

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
The addition of 5 and 15 results 20.
30
Syntax: print(function_name.__doc__)
3
The addition of 5 and 15 results 20.
32
Adding Docstring to the function

Python3

DocString

Chuỗi đầu tiên sau khi hàm được gọi là chuỗi tài liệu hoặc docString ngắn gọn. Điều này được sử dụng để mô tả chức năng của hàm. Việc sử dụng DocString trong các chức năng là tùy chọn nhưng nó được coi là một thực hành tốt.

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
43
The addition of 5 and 15 results 20.
0

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
46
False True
8

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
51
The addition of 5 and 15 results 20.
0

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
The addition of 5 and 15 results 20.
68

Đầu ra

Function to check if the number is even or odd

Ví dụ 2: Đối số từ khóa độ dài thay đổi

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
74
Function to check if the number is even or odd
9
Function to check if the number is even or odd
9
The addition of 5 and 15 results 20.
21

Syntax:  

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
0

The addition of 5 and 15 results 20.
3
The addition of 5 and 15 results 20.
01
The addition of 5 and 15 results 20.
24
Hello
Welcome
to
GeeksforGeeks
5
The addition of 5 and 15 results 20.
26

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
8
The addition of 5 and 15 results 20.
30
Syntax: print(function_name.__doc__)
3
The addition of 5 and 15 results 20.
32
Python Function Return Statement

Python3

DocString

Chuỗi đầu tiên sau khi hàm được gọi là chuỗi tài liệu hoặc docString ngắn gọn. Điều này được sử dụng để mô tả chức năng của hàm. Việc sử dụng DocString trong các chức năng là tùy chọn nhưng nó được coi là một thực hành tốt.

Cú pháp dưới đây có thể được sử dụng để in ra DocString của một hàm:

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
The addition of 5 and 15 results 20.
78
False True
5
The addition of 5 and 15 results 20.
84
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
28

Ví dụ: Thêm DocString vào chức năng

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
30

Python3

The addition of 5 and 15 results 20.
3
Hello
Welcome
to
GeeksforGeeks
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
33
Syntax: print(function_name.__doc__)
3
Hello
Welcome
to
GeeksforGeeks
7
even
odd
1
even
odd
1

Trả lời câu lệnh trong hàm Python

Câu lệnh trả về hàm được sử dụng để thoát khỏi hàm và quay lại người gọi hàm và trả lại giá trị hoặc mục dữ liệu được chỉ định cho người gọi. & NBSP;

False True
09

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
False True
11

Đầu ra

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
1

Ví dụ 2: Đối số từ khóa độ dài thay đổi

Python3

The addition of 5 and 15 results 20.
3
Hello
Welcome
to
GeeksforGeeks
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
33
Syntax: print(function_name.__doc__)
3
Hello
Welcome
to
GeeksforGeeks
7
even
odd
1
even
odd
1

Trả lời câu lệnh trong hàm Python

Câu lệnh trả về hàm được sử dụng để thoát khỏi hàm và quay lại người gọi hàm và trả lại giá trị hoặc mục dữ liệu được chỉ định cho người gọi. & NBSP;

False True
09

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
False True
11

Đầu ra

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
2

Câu lệnh trả về có thể bao gồm một biến, biểu thức hoặc hằng số được trả về cuối thực thi chức năng. Nếu không có điều nào ở trên có với câu lệnh trả về, một đối tượng không được trả về.

Python3

The addition of 5 and 15 results 20.
3
Hello
Welcome
to
GeeksforGeeks
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
33
Syntax: print(function_name.__doc__)
3
Hello
Welcome
to
GeeksforGeeks
7
even
odd
1
even
odd
1

Trả lời câu lệnh trong hàm Python

Câu lệnh trả về hàm được sử dụng để thoát khỏi hàm và quay lại người gọi hàm và trả lại giá trị hoặc mục dữ liệu được chỉ định cho người gọi. & NBSP;

False True
51

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
False True
53

Câu lệnh trả về có thể bao gồm một biến, biểu thức hoặc hằng số được trả về cuối thực thi chức năng. Nếu không có điều nào ở trên có với câu lệnh trả về, một đối tượng không được trả về. Try to guess the output of the following code. 

Python3

Ví dụ: Câu lệnh trả về chức năng Python

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
The addition of 5 and 15 results 20.
70

The addition of 5 and 15 results 20.
3
even
odd
6
The addition of 5 and 15 results 20.
73
Function to check if the number is even or odd
9
Function to check if the number is even or odd
9
Hello
Welcome
to
GeeksforGeeks
7

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
The addition of 5 and 15 results 20.
78
Hello
Welcome
to
GeeksforGeeks
7
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
28

Vượt qua tham chiếu hoặc vượt qua giá trị

Một điều quan trọng cần lưu ý là, trong Python, mỗi tên biến là một tài liệu tham khảo. Khi chúng ta chuyển một biến cho một hàm, một tham chiếu mới cho đối tượng sẽ được tạo. Tham số đi trong Python giống như tham chiếu trong Java.

False True
74

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
False True
53

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
False True
78

def function_name(parameter: data_type) -> return_type: """Doctring""" # body of the function return expression4 The addition of 5 and 15 results 20.87

The addition of 5 and 15 results 20.
3
The addition of 5 and 15 results 20.
89
Syntax: print(function_name.__doc__)
7
The addition of 5 and 15 results 20.
91
even
odd
1
The addition of 5 and 15 results 20.
93

Python3

Các

Khi chúng tôi vượt qua một tham chiếu và thay đổi tham chiếu nhận được đến một cái gì đó khác, kết nối giữa tham số được thông qua và nhận được bị phá vỡ. Ví dụ, hãy xem xét chương trình dưới đây như sau:

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
False True
96
False True
97
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
28

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
even
odd
00
False True
97
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
28

Các

Một ví dụ khác để chứng minh rằng liên kết tham chiếu bị hỏng nếu chúng ta gán một giá trị mới (bên trong hàm). & NBSP;

Python3

The addition of 5 and 15 results 20.
3
False True
15
even
odd
1
The addition of 5 and 15 results 20.
93

False True
15
even
odd
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
75

The addition of 5 and 15 results 20.
3
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
4
even
odd
11

first == Geeks
mid == for
last == Geeks
1
 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
7
even
odd
14

The addition of 5 and 15 results 20.
3
even
odd
16

even
odd
17

Đầu ra

 def function_name(parameter: data_type) -> return_type:
    """Doctring"""
    # body of the function
    return expression
3

Liên kết nhanh: & NBSP;

  • Đố về các chức năng Python
  • Sự khác biệt giữa phương pháp và chức năng trong Python
  • Chức năng hạng nhất trong Python
  • Các bài viết gần đây về các chức năng Python.

Các loại đối số trong Python là gì?

Trong Python, chúng ta có 4 loại đối số chức năng sau ...
Đối số mặc định ..
Đối số từ khóa (đối số được đặt tên).
Đối số vị trí ..
Các đối số tùy ý (đối số có độ dài thay đổi *args và ** kwargs).

3 loại đối số trong Python là gì?

Do đó, chúng tôi kết luận rằng các đối số chức năng Python và ba loại đối số của nó cho các chức năng.Đây là những đối số mặc định, từ khóa và tùy ý.default, keyword, and arbitrary arguments.

4 loại chức năng trong Python là gì?

Sau đây là các loại chức năng Python khác nhau:..
Chức năng tích hợp Python ..
Chức năng đệ quy Python ..
Chức năng Python Lambda ..
Các chức năng do người dùng Python xác định ..

Những lý lẽ trong Python là gì?

Tham số thuật ngữ và đối số có thể được sử dụng cho cùng một điều: thông tin được truyền vào một hàm.Từ quan điểm của một hàm: Một tham số là biến được liệt kê bên trong dấu ngoặc đơn trong định nghĩa hàm.Một đối số là giá trị được gửi đến hàm khi nó được gọi.the value that are sent to the function when it is called.