Every function is a closure?

Wikipedia says, that closure – is a function, which has an access to variables, declared outside of the function. There is even an example:

function startAt(x)
   function incrementBy(y)
       return x + y
   return incrementBy

variable closure1 = startAt(1)
variable closure2 = startAt(5)

But according to most programming languages (including python, javascript, swift, etc.) the next example is correct (written in python):

# Script starts
test = "check"

def func(test2):

    def func2():
        return test2 == test

    return func2()

print func("check") // returns TRUE
# Script ends

Basically, func is not a closure, but it obviously uses variable test, declared outside of the function. Does that mean func IS a closure?

Even in C++ you can run this:

std::string test = "check";

bool func(std::string test2) {
    if (test2 == test)
        return true;
    else
        return false;
}

int main() {
    if (func("check"))
        std::cout << "TRUE" << std::endl; // prints TRUE
}

Eventually, this makes every function being a closure. Where am I wrong?

7

No, not every function is a closure.

Wikipedia says:

… closure … is a function or reference to a function together with a referencing environment — a table storing a reference to each of the non-local variables (also called free variables or upvalues) of that function.

I’d add “non-local and non-global”, but the idea is correct.


Neither your C++ nor Python examples use closures. In both cases it’s just scoping rules allow functions to see their outer scope and global scope.


“Closure” happens in the 1st example – incrementBy is constructed in and then returned from it’s outer function, capturing argument x. When you assign variable closure1 = startAt(1), you end up having a closure (function) inside closure1 var which captured argument, which value happened to be 1, so when you call closure1(2) the result is 3 (1 + 2).

Think of it as memorizing some information about closure’s declaration scope: incrementBy retain a memory about insides of startAt, specifically a value of it’s argument x.

In lambda calculus, as I know, those “non-local” variables are called “free”, and functions with free variables are called “open terms”. Closure is a process of “closing” open terms by “fixing” values of those free variables in aforementioned “environment table”. Hence the name.


It’s worth noting that in Python and JS closure happens implicitly, while in PHP you have to explicitly tell which variables you want to close over (capture): http://php.net/manual/en/functions.anonymous.php – note use keyword in declarations:

// equivalent to the 1st example
function startAt($x) { //        vvvvvvvv          vv
    $incrementBy = function ($y) use ($x) { return $x + $y };
    return $incrementBy;
}

5

Closures are an efficient way to implement functions.

I claim that every function is conceptually a closure, even in the few languages which don’t have them.

The closed variables are then constants or static data inside the code. But in full-fledged closures (like in Ocaml, Scheme, Common Lisp, or C++11) the closed variables and the code are in the closure itself.

For instance, in C (and in C++98) functions are closures, but their closed (or free) variables are restricted to be static or global variables.

Closures also brings the possibility for anonymous functions, e.g. lambdas, which create closures with new closed variables (and that is something not possible in standard C). This is the abstraction operation of the λ-calculus (which does not exist in C, this is why every callback function -e.g. in GTK- takes some “client data” as an extra parameter).

From an implementation point of view, a function is always some code with some data required by the code. The data are the closed variables. For compiled C code, the data is wired in the code as fixed global or static variables (and changing these variables require recompilation), or as literal constants. So the only way of making an abstraction would be to regenerate some new code with some new variables in it.

Abstraction does not exist in standard C because there is no way to generate portably new functions. But you could use weird implementation-specific tricks. Imagine that you want to implement the translation generator function. In ocaml, the function to generate the translated-by delta function is

 let transl delta = fun x -> x + delta

and that is generating a new closure for every invocation. So in

 let t3 = transl 3 in t3 5

a new closure t3 is generated (but perhaps the optimizer is removing it) and the result is 8 (which is 3+5).

You could do insane tricks in C: on Linux, you would for example generate at runtime a new textual file t3mod.c containing

 int t3(int y) { return y+3; };

and you would compile that t3mod.c file in a shared object t3mod.so and dlopen it then dlsym it using "t3". This contrived way (using C code generation at runtime, then dynamically loading it) is a way of implementing abstractions in C and it is a way to generate dynamically new function pointers. Likewise, you might use JIT compiling libraries like e.g. libjit, LLVM (or libgccjit in future GCC 5). Read also about partial evaluation & eval & multi-stage programming (e.g. Meta OCaml…). See also J.Pitrat’s blog entries on meta-combinatorial search & meta-bugs.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật