Cải thiện bài viết
Lưu bài viết
Điều kiện tiên quyết: Các chức năng trong Pythonin Python, bất kỳ chức năng bằng văn bản nào cũng có thể được gọi bởi một chức năng khác. Lưu ý rằng đây có thể là cách phá vỡ một vấn đề thanh lịch nhất thành các vấn đề nhỏ. Trong bài viết này, chúng ta sẽ tìm hiểu làm thế nào chúng ta có thể gọi một hàm được xác định từ một hàm khác với sự trợ giúp của nhiều ví dụ. & NBSP; Functions in Python
In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function
with help of multiple examples.
Gọi và gọi chức năng? & Nbsp; Hàm gọi một hàm khác được gọi là hàm và hàm gọi được gọi bởi một hàm khác là chức năng gọi.
The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function.
Làm thế nào để thực thi chức năng hoạt động? & Nbsp; một cấu trúc dữ liệu ngăn xếp được sử dụng trong quá trình thực hiện các cuộc gọi chức năng. Bất cứ khi nào một hàm được gọi thì hàm gọi được đẩy vào ngăn xếp và được gọi là chức năng được thực thi. Khi hàm được gọi hoàn thành thực thi và trả về thì hàm gọi được bật ra từ ngăn xếp và thực thi. Thực thi chức năng gọi sẽ chỉ được hoàn thành khi chức năng được gọi là hoàn thành thực thi.
A stack data structure is used during the execution of the function calls. Whenever a function is invoked then the calling function is pushed into the
stack and called function is executed. When the called function completes its execution and returns then the calling function is popped from the stack and executed. Calling Function execution will be completed only when called Function is execution completes.
Trong hình dưới đây. Cuộc gọi chức năng được thực hiện từ hàm chính sang chức năng1, bây giờ trạng thái của hàm chính được lưu trữ trong ngăn xếp và thực hiện hàm chính được tiếp tục khi hàm 1 trả về. FUCNTION1 gọi hàm2 Bây giờ trạng thái của hàm1 được lưu trữ ngăn xếp và thực thi hàm 1 sẽ được tiếp tục khi hàm 2 trả về. & Nbsp;
Hãy xem xét ví dụ dưới đây của cuộc gọi chức năng. Hàm hàm sumofsquares gọi hàm vuông trả về bình phương của số. & Nbsp;
Python3
def
Square[X]:
return
[X
*
X]
def
Function2 : Hello Function1 : World1
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World5
Function2 : Hello Function1 : World7
Function2 : Hello Function1 : World8
Function2 : Hello Function1 : World9
Function2 : Hello Function1 : World0
Function2 : Hello Function1 : World1
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World5
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World8
Function2 : Hello Function1 : World4
def
0
return
Function2 : Hello Function1 : World3
Các
7
Function2 : Hello Function1 : World4
9return
0return
1
Function2 : Hello Function1 : World4
return
3return
4return
5
Function2 : Hello Function1 : World3
return
7return
8 return
9Đầu ra: & nbsp;
Sum of the Square of List of Numbers: 385
Gọi chức năng từ một hàm khác trong cùng một lớp trong ví dụ dưới đây, chức năng Phương thức lớp1 gọi Phương thức Phương thức2 từ lớp.
In the below example, the class method Function1 calls method Function2 from the class.
Python3
[X
0 [X
1
def
[X
4[X
55____76
Function2 : Hello Function1 : World2
[X
5[X
9Function2 : Hello Function1 : World4
*
1Function2 : Hello Function1 : World2
[X
5*
4Function2 : Hello Function1 : World4
*
6
def
*
9__7555
Function2 : Hello Function1 : World2
[X
5X]
4Function2 : Hello Function1 : World2
return
4X]
7[X
5X]
9Function2 : Hello Function1 : World22
def
Function2 : Hello Function1 : World04
[X
55____76Function2 : Hello Function1 : World2
return
4Function2 : Hello Function1 : World09
[X
5Function2 : Hello Function1 : World11
Function2 : Hello Function1 : World2
return
Function2 : Hello Function1 : World14
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World16
Function2 : Hello Function1 : World14
Function2 : Hello Function1 : World18
Đầu ra: & nbsp;
Function2 : Hello Function1 : World
Gọi chức năng từ một hàm khác trong cùng một lớp trong ví dụ dưới đây, chức năng Phương thức lớp1 gọi Phương thức Phương thức2 từ lớp.parent class Function from Child class Function –
Consider the below example the child class method invokes the parent class method. The child class
inherits the attributes from the parent class.
Python3
[X
0 [X
1
def
[X
4[X
55____76
Function2 : Hello Function1 : World2
[X
5[X
9Function2 : Hello Function1 : World4
*
1Function2 : Hello Function1 : World2
[X
5*
4Function2 : Hello Function1 : World4
*
6
def
Function2 : Hello Function1 : World04
[X
55____76Function2 : Hello Function1 : World2
return
4Function2 : Hello Function1 : World09
[X
5Function2 : Hello Function1 : World11
Function2 : Hello Function1 : World2
return
Function2 : Hello Function1 : World14
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World16
def
*
9__7555
Function2 : Hello Function1 : World2
[X
5X]
4Function2 : Hello Function1 : World22
Function2 : Hello Function1 : World2
return
def
Function2 : Hello Function1 : World04
[X
55____76Function2 : Hello Function1 : World14
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World16
Function2 : Hello Function1 : World71
Đầu ra: & nbsp;
Function2 : Hello Function1 : World