Cprogramming 简明教程

Nested Functions in C

在编程语境中,术语 nesting 指将一个特定编程元素包含在另一个类似元素中。就如同嵌套循环、嵌套结构等,嵌套函数是一个术语,用于描述在一个函数中使用一个或多个函数。

The term nesting, in programming context refers to enclosing a particular programming element inside another similar element. Just like nested loops, nested structures, etc., a nested function is a term used to describe the use of one or more functions inside another function.

What is Lexical Scoping?

在 C 语言中,不可能在另一个函数中定义一个函数。简而言之,C 中不支持嵌套函数。函数只能在另一个函数中成为 declared (而不是 defined )。

In C language, defining a function inside another one is not possible. In short, nested functions are not supported in C. A function may only be declared (not defined) within another function.

当一个函数在另一个函数中声明时,它被称为 lexical scoping 。词法作用域在 C 中无效,因为编译器无法访问内部函数的正确内存位置。

When a function is declared inside another function, it is called lexical scoping. Lexical scoping is not valid in C because the compiler cannot reach the correct memory location of inner function.

Nested Functions Have Limited Use

嵌套函数定义无法访问周围块中的局部变量。它们只能访问全局变量。在 C 中,有两个嵌套作用域: localglobal 。因此,嵌套函数的用途受到限制。

Nested function definitions cannot access local variables of surrounding blocks. They can access only global variables. In C, there are two nested scopes: local and global. So, nested functions have limited use.

Example: Nested Function

如果你想创建一个类似于下面所示的嵌套函数,那么它将产生一个错误 −

If you want to create a nested function like the one shown below, then it will generate an error −

#include <stdio.h>

int main(void){

   printf("Main Function");

   int my_fun(){

      printf("my_fun function");

      // Nested Function
      int nested(){
         printf("This is a nested function.");
      }
   }
   nested();
}

在运行这段代码时,你会收到一个错误 −

On running this code, you will get an error −

main.c:(.text+0x3d): undefined reference to `nested'
collect2: error: ld returned 1 exit status

Trampolines for Nested Functions

嵌套函数作为“GNU C”中的一个扩展受到支持。GCC 通过使用称为 trampolines 的技术实现获取嵌套函数的地址。

Nested functions are supported as an extension in "GNU C". GCC implements taking the address of a nested function using a technique called trampolines.

在声明中,它要求函数以上述关键字 auto 为前缀。

A trampoline is a piece of code created at runtime when the address of a nested function is taken. It requires the function to be prefixed with the keyword auto in the declaration.

Example 1

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   auto int nested();
   nested();

   printf("In Main Function now\n");

   int nested(){
      printf("In the nested function now\n");
   }

   printf("End of the program");
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

In the nested function now
In Main Function now
End of the program

Example 2

在该程序中,一个函数 square() 嵌套在另一个函数 myfunction() 中。嵌套函数使用 auto 关键字进行声明。

In thi program, a function square() is nested inside another function myfunction(). The nested function is declared with the auto keyword.

#include <stdio.h>
#include <math.h>

double myfunction (double a, double b);

int main(){
   double x = 4, y = 5;
   printf("Addition of squares of %f and %f = %f", x, y, myfunction(x, y));
   return 0;
}

double myfunction (double a, double b){
   auto double square (double c) { return pow(c,2); }
   return square (a) + square (b);
}

运行代码并检查其输出:

Run the code and check its output −

Addition of squares of 4.000000 and 5.000000 = 41.000000

Nested Functions: Points to Note

在使用嵌套函数时需要注意以下几点 −

One needs to be aware of the following points while using nested functions −

  1. A nested function can access all the identifiers of the containing function that precede its definition.

  2. A nested function must not be called before the containing function exits.

  3. A nested function cannot use a goto statement to jump to a label in the containing function.

  4. Nested function definitions are permitted within functions in any block, mixed with the other declarations and statements in the block.

  5. If you try to call a nested function through its address after the containing function exits, it throws an error.

  6. A nested function always has no linkage. Declaring one with "extern" or "static" always produces errors.