Cprogramming 简明教程

Return Statement in C

return 语句终止函数执行,并将控制权返回给调用函数。每个函数都应在其最后一条语句中包含一条 return 语句。在使用 returns 语句时,返回类型和返回值(表达式)必须相同。

The return statement terminates the execution of a function and returns control to the calling function. Every function should have a return statement as its last statement. While using the returns statement, the return type and returned value (expression) must be the same.

Syntax of return Statement

以下是 return 语句的语法:

Here is the syntax of the return statement:

return value_or_expression;

以下 main() 函数将其最后一条语句显示为返回 −

The following main() function shows return as its last statement −

int main(){
   // function body;
   return 0;
}

main() function 返回 0 表示函数成功完成。要表示函数失败,则返回非零表达式。

The main() function returning 0 indicates the successful completion of the function. To indicate failure of the function, a non−zero expression is returned.

The void return statement

function 的返回类型可以是 void。在那种情况下,return 语句是可选的。可以省略它,或者在未使用任何表达式时返回。

A function's return type can be void. In such a case, return statement is optional. It may be omitted, or return without any expression is used.

Example

#include <stdio.h>
/* function declaration */
void test(){
   return;
}
int main() {
   test();
   printf("end");
   return 0;
}

Return type mismatch in return statement

程序中的每个函数都必须对其原型进行前向声明。默认情况下,每个函数都返回一个整数。然而,不带原型的其他返回类型的函数是不可接受的。

Each function in the program must have a forward declaration of its prototype. By default, each function returns an integer. However, function of other return types without prototype is not accepted.

Example

int  main(){
   test(5);
   printf("end");
   return 0;
}
float test(int a) {
   return 1.1 ;
}

Output

Error: C:\Users\mlath\test.c|12|error: conflicting types for 'test'

这是因为,没有原型的函数被假定为 int 类型,这与定义冲突。

This is because, function without prototype is assumed as of int type, which conflicts with the definition.

如果原型中的函数的返回类型与返回表达式的类型不匹配,也会出现同样的错误,如下所示报告错误:

The same error occurs if the return type of a function in the prototype doesn’t match with the type of return expression, an error is reported as below −

float test(int);
int  main(){
   test(5);
   printf("end");
   return 0;
}
float test(float a){
   return 1.1 ;
}

Multiple return values with return statement

可以定义具有多个参数的函数,但只能返回一个值。然而,您可以使用如下所示的多个条件返回语句:

A function can be defined with more than one arguments, but can return only one value. You can however use multiple conditional return statements as shown below −

Example

int test(int);
int  main() {
   test(5);
   printf("end");
   return 0;
}

int test(int a){
   if (a<3)
      return 1;
   else
      return 0;
}

Function returning an array

不可能将整个 array 作为参数返回给函数。但是,可以通过指定数组的名称而不带索引来返回指向数组的指针。

It is not possible to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array’s name without an index.

Example

以下程序演示了如何将数组传递给在执行特定进程后返回数组的函数。

The following program shows how to pass an array to a function that returns an array after performing a certain process.

#include <stdio.h>
int* test(int *);
int  main(){
   int a[] = {1,2,3,4};
   int i;
   int *b = test(a);
   for (i=0; i<4; i++){
      printf("%d\n", b[i]);
   }
   return 0;
}
int * test(int*a){
   int i;
   for (i=0; i<4; i++){
      a[i] = 2*a[i];
   }
   return a;
}
2
4
6
8

函数只能使用返回语句返回一个值。要返回多个值,我们使用指针或结构

function can only return a single value using return statement. To return multiple values, we use pointers or structures

exit() instead of return statement

return 语句不同, exit() function 也用于终止程序的执行而不将控制权转回调用函数。当程序执行完毕或发生无法恢复的错误时,在函数中使用它。这是处理 C 中异常错误的标准方法。调用 exit() 时,程序控制权不会返回到调用 exit() 的点。相反,控制权将传回操作系统。

Unlike the return statement, the exit() function is also used to terminate the execution of the program without transferring the control back to the calling function. It is used inside a function when the program has finished its execution or when an unrecoverable error occurs. It is a standard way of handling exception errors in C. When exit() is called, the program control does not return to the point where exit() was invoked. Instead, the control is handed back to the operating system.

exit() 函数是库函数,在 stdlib.h 头文件中定义。

The exit() function is library function defined in stdlib.h header file.

Syntax

void exit(int status);

exit() 通常从 main() 函数或任何其他函数调用,以终止整个程序。

exit() is typically called from the main() function or any other function to terminate the entire program.

它包含在 <stdlib.h> header file 中。

It is included in the <stdlib.h> header file.

由于它会导致程序终止,exit() 不会直接向调用函数返回一个值。相反,它终止程序并返回一个状态码。它是一个整数,表示程序的退出状态,指示成功或失败。

Since it results in termination of the program, exit() does not return a value directly to the caller function. Instead, it terminates the program and returns a status code. It is an integer that represents the exit status of the program, indicating success or failure.