Computer Programming 简明教程
Computer Programming - Functions
函数是一个有组织的、可重复使用的代码块,用于执行单个相关操作。函数为应用程序提供更好的模块化和高度的代码重复使用。你已经看到了各种函数,如 printf() 和 main() 。这些是由语言本身提供的内置函数,但我们也可以编写自己的函数,本教程将教你如何用 C 语言编写和使用这些函数。
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. You have already seen various functions like printf() and main(). These are called built-in functions provided by the language itself, but we can write our own functions as well and this tutorial will teach you how to write and use those functions in C programming language.
函数的一个好处是它们以不同的名称而出名。不同的编程语言对它们的命名也不同,例如,函数、方法、子例程、过程等。如果你遇到任何这样的术语,那么请想象一下同样的概念,这是我们将在本教程中讨论的。
Good thing about functions is that they are famous with several names. Different programming languages name them differently, for example, functions, methods, sub-routines, procedures, etc. If you come across any such terminology, then just imagine about the same concept, which we are going to discuss in this tutorial.
让我们从一个程序开始,在其中定义两个数字数组,然后从每个数组中找出最大的数字。以下是从给定数字集中找出最大数字的步骤:
Let’s start with a program where we will define two arrays of numbers and then from each array, we will find the biggest number. Given below are the steps to find out the maximum number from a given set of numbers −
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 语言:
Let’s translate the above program in C programming language −
#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 );
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Max in first set = 50
Max in second set = 501
如果你对上面的示例很清楚,那么就很容易理解为什么我们需要一个函数。在上面的示例中,只有两个数字集,set1 和 set2,但考虑一种情况,我们有 10 个或更多类似的数字集,要从每个集中找出最大的数字。在这种情况下,我们将不得不重复处理 10 次或更多次,最终,该程序将因重复的代码而变得过大。为了处理这种情况,我们编写自己的函数,在其中尝试保留将在我们的编程中反复使用的源代码。
If you are clear about the above example, then it will become easy to understand why we need a function. In the above example, there are only two sets of numbers, set1 and set2, but consider a situation where we have 10 or more similar sets of numbers to find out the maximum numbers from each set. In such a situation, we will have to repeat, processing 10 or more times and ultimately, the program will become too large with repeated code. To handle such situation, we write our functions where we try to keep the source code which will be used again and again in our programming.
现在,让我们看看如何在 C 编程语言中定义一个函数,然后在后续部分中,我们将解释如何使用它们。
Now, let’s see how to define a function in C programming language and then in the subsequent sections, we will explain how to use them.
Defining a Function
C 编程语言中函数定义的常规形式如下 -
The general form of a function definition in C programming language is as follows −
return_type function_name( parameter list ) {
body of the function
return [expression];
}
C 编程中的函数定义包括函数头和函数体。以下是函数的所有部分 -
A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −
-
Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
-
Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
-
Parameter List − A parameter is like a placeholder. When a function is invoked, you pass a value as a parameter. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
-
Function Body − The function body contains a collection of statements that defines what the function does.
Calling a Function
在创建 C 函数时,你给出了该函数要做什么的定义。要使用函数,你必须调用该函数来执行定义的任务。
While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform a defined task.
现在,让我们在函数的帮助下编写上述示例:
Now, let’s write the above example with the help of a function −
#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 );
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Max in first set = 50
Max in second set = 501
Functions in Java
如果你对 C 编程中的函数很清楚,那么在 Java 中也容易理解它们。Java 编程语言将它们命名为 methods ,但其余概念仍然或多或少相同。
If you are clear about functions in C programming, then it is easy to understand them in Java as well. Java programming names them as methods, but the rest of the concepts remain more or less same.
以下是用 Java 编写的等效程序。你可以尝试执行它以查看输出:
Following is the equivalent program written in Java. You can try to execute it to see the output −
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;
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Max in first set = 50
Max in second set = 501
Functions in Python
再一次,如果你知道 C 和 Java 编程中的函数概念,那么 Python 也没有什么不同。以下是 Python 中定义函数的基本语法:
Once again, if you know the concept of functions in C and Java programming, then Python is not much different. Given below is the basic syntax of defining a function in Python −
def function_name( parameter list ):
body of the function
return [expression]
使用 Python 中的此函数语法,上面的示例可以这样编写 −
Using this syntax of function in Python, the above example can be written as follows −
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
执行上述代码后,将生成以下结果 −
When the above code is executed, it produces the following result −
Max in first set = 50
Max in second set = 501