函数是一个有组织的,可重用的代码块,用于执行单个相关操作.函数为您的应用程序提供了更好的模块化,并提供了高度的代码重用.您已经看过各种函数,如 printf()和 main().这些被称为语言本身提供的内置函数,但我们也可以编写自己的函数,本教程将教你如何用C编程语言编写和使用这些函数.
关于功能的好处是它们以多个名字而闻名.不同的编程语言对它们的命名方式不同,例如,函数,方法,子例程,过程等.如果您遇到任何此类术语,那么请想象一下我们将在本教程中讨论的相同概念.
让我们从一个程序开始,我们将定义两个数字数组,然后从每个数组中,我们将找到最大的数字.下面给出了从给定的一组数字中找出最大数量的步骤 :
1. Get a list of numbers L1, L2, L3....LN2. Assume L1 is the largest, Set max = L13. Take next number Li from the list and do the following 4. If max is less than Li5. Set max = Li6. 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
如果你清楚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
再一次,如果您了解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