Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Wikipedia says, that closure - is a function, which has an access to variables,

ID: 647874 • Letter: W

Question

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 language (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 (test == 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?

Explanation / Answer

No, not every function is a closure.

Wikipedia says:

... closure ... is a function or reference to a function together with a referencing environment

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote