Nested Functions in C

Source: Internet
Author: User

Nested Functions, also known as closure, belongs to the concept of functional language and has always thought that closure is not supported in C. Now it seems that I am wrong, but it is not supported in C standard, but GCC supports it.

 

Since GCC supports closure, lexical scoping is also supported. label in C can also be freely redirected in nested functions, and the nested function can be called externally as the return address, this is consistent with the concept in lisp.

 

C code
Foo (double a, double B)
{
Double square (double z) {return z * z ;}

Return square (a) + square (B );
}
 
 
Bar (int * array, int offset, int size)
{
Int access (int * array, int index)
{Return array [index + offset];}
Int I;
/*...*/
For (I = 0; I <size; I ++)
/*... */Access (array, I )/*...*/
}
 
 
Bar (int * array, int offset, int size)
{
_ Label _ failure;
Int access (int * array, int index)
{
If (index> size)
Goto failure;
Return array [index + offset];
}
Int I;
/*...*/
For (I = 0; I <size; I ++)
/*... */Access (array, I )/*...*/
/*...*/
Return 0;

/* Control comes here from access
If it detects an error .*/
Failure:
Return-1;
}

 


From bookjovi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.