计算机程序设计 - 函数



函数是一块组织良好的、可重用的代码块,用于执行单个相关的操作。函数为您的应用程序提供了更好的模块化和高度的代码重用性。您已经看到了各种函数,例如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.