計算機程式設計 - 函式



函式是一塊組織良好的、可重用的程式碼塊,用於執行單個相關的操作。函式為您的應用程式提供了更好的模組化和高度的程式碼重用性。您已經看到了各種函式,例如printf()main()。這些被稱為內建函式,由語言本身提供,但我們也可以編寫自己的函式,本教程將教您如何在 C 程式語言中編寫和使用這些函式。

函式的好處在於它們有很多不同的名稱。不同的程式語言對它們的稱呼不同,例如函式、方法、子例程、過程等。如果您遇到任何此類術語,只需想象一下我們將在此教程中討論的相同概念。

讓我們從一個程式開始,在這個程式中,我們將定義兩個數字陣列,然後從每個陣列中找到最大的數字。以下是找出給定數字集中最大數字的步驟:

1. Get a list of numbers L1, L2, L3....LN
2. Assume L1 is the largest, Set max = L1
3. Take next number Li from the list and do the following
4.    If max is less than Li
5.       Set max = Li
6.    If Li is last number from the list then
7.       Print value stored in max and come out
8. Else prepeat same process starting from step 3

讓我們將上面的程式翻譯成 C 程式語言:

#include <stdio.h>

int main() {
   int set1[5] = {10, 20, 30, 40, 50};
   int set2[5] = {101, 201, 301, 401, 501};
   int i, max;
   
   /* Process first set of numbers available in set1[] */
   max = set1[0];
   i = 1;    
   while( i < 5 ) {
      if( max <  set1[i] ) {
         max = set1[i];
      }
      i = i + 1;
   }
   
   printf("Max in first set = %d\n", max );
    
   /* Now process second set of numbers available in set2[] */
   max = set2[0];
   i = 1;    
   while( i < 5 ) {
      if( max <  set2[i] ) {
         max = set2[i];
      }
      i = i + 1;
   }
   printf("Max in second set = %d\n", max );
}

編譯並執行上述程式碼後,將產生以下結果:

Max in first set = 50
Max in second set = 501

如果您清楚地瞭解了上面的示例,那麼就很容易理解為什麼我們需要函式。在上面的示例中,只有兩組數字,set1 和 set2,但是考慮一下我們有 10 個或更多類似的數字集需要找出每組中的最大數字的情況。在這種情況下,我們將不得不重複處理 10 次或更多次,最終程式將變得太大,並且程式碼重複。為了處理這種情況,我們編寫函式,在函式中,我們嘗試保留將在我們的程式設計中反覆使用的原始碼。

現在,讓我們看看如何在 C 程式語言中定義函式,然後在後續部分,我們將解釋如何使用它們。

定義函式

C 程式語言中函式定義的一般形式如下:

return_type function_name( parameter list ) {
   body of the function
   
   return [expression];
}

C 程式設計中的函式定義由一個函式頭和一個函式體組成。以下是函式的所有部分:

  • 返回型別 - 函式可以返回值。return_type 是函式返回值的資料型別。有些函式執行所需的操作而不返回值。在這種情況下,return_type 是關鍵字void

  • 函式名 - 這是函式的實際名稱。函式名和引數列表一起構成函式簽名。

  • 引數列表 - 引數就像一個佔位符。呼叫函式時,您將值作為引數傳遞。此值被稱為實際引數或引數。引數列表指的是函式的引數的型別、順序和數量。引數是可選的;也就是說,函式可能不包含任何引數。

  • 函式體 - 函式體包含定義函式作用的一組語句。

呼叫函式

在建立 C 函式時,您會給出函式必須執行的操作的定義。要使用函式,您必須呼叫該函式以執行定義的任務。

現在,讓我們使用函式編寫上面的示例:

#include <stdio.h>

int getMax( int set[] ) {
   int i, max;
   
   max = set[0];
   i = 1;    
   while( i < 5 ) {
      if( max <  set[i] ) {
         max = set[i];
      }
      i = i + 1;
   }
   return max;
}
main() {
   int set1[5] = {10, 20, 30, 40, 50};
   int set2[5] = {101, 201, 301, 401, 501};
   int max;

   /* Process first set of numbers available in set1[] */
   max = getMax(set1);
   printf("Max in first set = %d\n", max );
    
   /* Now process second set of numbers available in set2[] */
   max = getMax(set2);
   printf("Max in second set = %d\n", max );
}

編譯並執行上述程式碼後,將產生以下結果:

Max in first set = 50
Max in second set = 501

Java 中的函式

如果您瞭解 C 程式設計中的函式,那麼在 Java 中也很容易理解它們。Java 程式設計將它們稱為方法,但其餘的概念大致相同。

以下是使用 Java 編寫的等效程式。您可以嘗試執行它以檢視輸出:

public class DemoJava {
   public static void main(String []args) {
      int[] set1 = {10, 20, 30, 40, 50};
      int[] set2 = {101, 201, 301, 401, 501};
      int max;

      /* Process first set of numbers available in set1[] */
      max = getMax(set1);
      System.out.format("Max in first set = %d\n", max );

      /* Now process second set of numbers available in set2[] */
      max = getMax(set2);
      System.out.format("Max in second set = %d\n", max );
   }
   public static int getMax( int set[] ) {
      int i, max;
      max = set[0];
      i = 1;    
      
      while( i < 5 ) {
         if( max <  set[i] ) {
            max = set[i];
         }
         i = i + 1;
      }
      return max;
   }
}

執行上述程式後,將產生以下結果:

Max in first set = 50
Max in second set = 501

Python 中的函式

再次,如果您瞭解 C 和 Java 程式設計中的函式概念,那麼 Python 並沒有太大區別。以下是 Python 中定義函式的基本語法:

def function_name( parameter list ):
   body of the function
   
   return [expression]

使用 Python 中的這種函式語法,上面的示例可以寫成如下:

def getMax( set ):
   max = set[0]
   i = 1   
   
   while( i < 5 ):
      if( max <  set[i] ):
         max = set[i]
      
      i = i + 1
   return max

set1 = [10, 20, 30, 40, 50]
set2 = [101, 201, 301, 401, 501]

# Process first set of numbers available in set1[]
max = getMax(set1)
print "Max in first set = ", max
    
# Now process second set of numbers available in set2[]
max = getMax(set2)
print "Max in second set = ", max

執行上述程式碼後,將產生以下結果:

Max in first set =  50
Max in second set =  501
廣告
© . All rights reserved.