目录

计算机编程 - 功能(Functions)

函数是有组织的可重用代码块,用于执行单个相关操作。 函数为您的应用程序提供了更好的模块化,并且可以高度重用代码。 您已经看过各种函数,如printf()main() 。 这些被称为语言本身提供的内置函数,但我们也可以编写自己的函数,本教程将教你如何用C编程语言编写和使用这些函数。

关于功能的好处是它们以多个名字而闻名。 不同的编程语言对它们的命名方式不同,例如,函数,方法,子例程,过程等。如果您遇到任何此类术语,那么只需想象一下我们将在本教程中讨论的相同概念。

让我们从一个程序开始,我们将定义两个数字数组,然后从每个数组中,我们将找到最大的数字。 以下是从一组给定数字中找出最大数量的步骤 -

1. Get a list of numbers L<sub>1</sub>, L<sub>2</sub>, L<sub>3</sub>....L<sub>N</sub>
2. Assume L<sub>1</sub> is the largest, Set max = L<sub>1</sub>
3. Take next number L<sub>i</sub> from the list and do the following
4.    If max is less than L<sub>i</sub>
5.       Set max = L<sub>i</sub>
6.    If L<sub>i</sub> 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编程语言定义一个函数,然后在后面的章节中,我们将解释如何使用它们。

定义一个函数 (Defining a Function)

C编程语言中函数定义的一般形式如下 -

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

C编程中的函数定义由function headerfunction body 。 以下是函数的所有部分 -

  • Return Type - 函数可以返回值。 return_type是函数返回的值的数据类型。 某些函数执行所需的操作而不返回值。 在这种情况下,return_type是关键字void

  • Function Name - 这是Function Name的实际名称。 函数名称和参数列表一起构成函数签名。

  • Parameter List - 参数类似于占位符。 调用函数时,将值作为参数传递。 该值称为实际参数或参数。 参数列表是指函数参数的类型,顺序和数量。 参数是可选的; 也就是说,函数可能不包含任何参数。

  • Function Body - 函数体包含一组语句,用于定义函数的功能。

调用一个函数 (Calling a Function)

在创建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编程将它们命名为methods ,但其余概念或多或少相同。

以下是用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
↑回到顶部↑
WIKI教程 @2018