Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error with functions returning functions #99

Open
Atybot opened this issue Mar 4, 2023 · 1 comment
Open

error with functions returning functions #99

Atybot opened this issue Mar 4, 2023 · 1 comment

Comments

@Atybot
Copy link
Contributor

Atybot commented Mar 4, 2023

function func1(arg1, arg2) {
  return function nestedfunc() {
    return arg1 + arg2;
  };
}

console.log(func1(1, 2)());
@HotoRas
Copy link

HotoRas commented Jun 16, 2024

You could do it with external struct defines as static in c:

#include <stdio.h>

struct Template { int a1; int a2; };

static struct Template temp;

int nestedfunc(void) {
    return temp.a1 + temp.a2;
}

int* (* func1(int arg1, int arg2))(void) {
    temp.a1 = arg1; temp.a2 = arg2;
    return (int* (*) (void)) nestedfunc;
}

int main() {
    temp.a1 = 0; temp.a2 = 0;

    printf("%d\n", func1(1, 2)());
    return 0;
}

However, it's unable in ISO C/C++ to directly nest functions- it's GCC/G++ feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants