📜  计算机编程-函数

📅  最后修改于: 2021-01-18 06:27:52             🧑  作者: Mango


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

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 

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