正如我們所知,函數是用於在編程中執行某些特定任務的語句塊。這也有助於將大量代碼分解成更小的塊或模塊。函數可以在任何地方被調用,也可以在程序中被調用多少次。它允許我們通過簡單地調用程序中的特定函數或塊來重用代碼。因此,它避免了相同代碼的重複。我們可以在類、模塊、嵌套函數等內部定義函數。
函數的特徵
以下是 Python 函數的特性:
- 它用於避免代碼重複。
- 使用函數,我們可以將一組代碼分成更小的模塊。
- 它有助於隱藏代碼和創建清晰的理解模塊。
- 它允許代碼可重用,從而節省內存。
- 函數內部編寫的語句只能用函數名執行。
- Python 函數以 def 開頭,然後是冒號( : )後跟函數名。
定義函數的規則
- 在 Python 函數中使用 def 關鍵字來聲明和定義函數。
- 函數名必須以下列標識符開頭,如:a- z、A-Z 和下劃線(
_
)。 - 每個函數必須跟在冒號(:)之後,然後縮進才能編寫程序。
- 在 Python 函數中,保留字不能用作函數名或標識符。
- 在 Python 中,函數參數可以為空或倍數。
用 Python 創建一個函數
要創建一個函數,我們需要使用 def 關鍵字在 Python 中聲明或編寫一個函數。以下是創建函數的語法:
語法
def function_name(): # use def keyword to define the function
Statement to be executed
return statement # return a single value.
讓我們用 Python 創建一個函數程序。
Myfun.py
def myFun(): # define function name
print(" Welcome to JavaTpoint")
myFun() # call to print the statement
輸出:
Welcome to JavaTpoint
Python 中的函數調用
一旦在 Python 中創建了一個函數,我們可以通過編寫 function_name() 本身或者另一個函數/嵌套函數來調用它。下面是調用函數的語法。
語法:
def function_name():
Statement1
function_name() # directly call the function
# calling function using built-in function
def function_name():
str = function_name('john') # assign the function to call the function
print(str) # print the statement
考慮以下示例,使用 Python 中的函數打印歡迎消息。
CallFun.py
def MyFun():
print("Hello World")
print(" Welcome to the JavaTpoint")
MyFun() # Call Function to print the message.
輸出:
Hello World
Welcome to the JavaTpoint
在上面的例子中,我們調用了打印語句的 MyFun() 函數。
在 Python 中調用嵌套函數
當我們在一個函數內部構造另一個函數時,它被稱為嵌套函數。我們可以使用 def 關鍵字創建嵌套函數。創建函數後,我們必須調用外部函數和內部函數來執行語句。讓我們創建一個程序來理解嵌套函數的概念以及我們如何調用這些函數。
巢。py
def OutFun(): # outer function
print("Hello, it is the outer function")
def InFun(): # inner function
print("Hello, It is the inner function")
InFun() # call inner
OutFun() # call outer function
輸出:
Hello, it is the outer function
Hello, it is the inner function
從上面的例子中我們可以看到, InFun ()函數是在 OutFun()函數內部定義的。要調用的函數,我們首先調用程序中的的函數。之後 OutFun ()函數將開始執行,然後調用 InFun()作為上面的輸出。
注意:要調用內部函數,必須先調用外部函數。如果不調用外部函數,內部函數將不會被執行。
用 Python 中的嵌套函數打印兩個數相乘的程序。
巢 _arg.py
def fun1(): # outer function
a = 6 # define variable
def fun2(b): # inner function
a = 4 # inner variable
print ("Display the sum of inner function", a + b) # sum of inner function
print ("Display the value of outer variable", a) # it displays the value of outer function fun2(4) # Inner function
輸出:
Display the value of outer variable 6
Display the sum of inner function 8
作為一級對象的功能
在 Python 中,函數是一級對象。因為它將視為與對象相同,並且具有與對象相同的屬性和方法。可以將函數賦給變量,將它們作為參數傳遞,將它們存儲在數據結構中,並從其他函數返回值。它可以被操縱,比如 Python 中的其他對象。此外,Python 程序中的所有數據都表示在對象或關係中。因此它也被稱為 Python 函數的一級公民。
一類函數的性質
- 函數可以分配給變量
- 函數是對象類型的一個例子。
- 我們也從函數中返回函數。
- 函數與對象具有相同的屬性和方法
- 該函數被視為一個對象,作為參數傳遞給另一個函數。
創建一個程序,將 Python 函數理解為一個對象。
Obj.py
def MyObject(text): # Pass an argument.
return text.upper()
# Call the function inside the print() function.
print (MyObject("Welcome to JavaTpoint"))
str = MyObject # assign the function to a variable
# call the function using the str variable.
print (str("Hello, Welcome to JavaTpoint"))
輸出:
WELCOME TO JAVATPOINT
HELLO, WELCOME TO JAVATPOINT
編寫一個程序來調用類內的函數。
學生 py
class Student:
Roll_no = 101
name = "Johnson"
def show(self):
print(" Roll no. is %d\nName of student is %s" % (self.Roll_no, self.name))
stud = Student() # Create the stud object of Student class
stud.show() # call the function using stud object.
輸出:
Roll no. is 101
Name of student is Johnson
原創文章,作者:XJKOO,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130254.html