Search code examples
c++cfunctionstatements

How to define a function with a statement block in C or C++?


So as the title suggests I'm interested in defining a function that has a statement block. What this means is best explained via example:

    for(int i=0;i<10;i++){
    <statement block>
    }

How could I define a function that has a block like that?

If this isn't possible in C nor C++ I'm really interested in which (if any) languages this is possible. Thanks in advance!

EDIT:

it's come to my understanding that I wasn't clear enough, let me elaborate.

I would like to define a function like this:

    int foo(){
        printf("foo");
        return 0;
    }

and then use it in main like so:

    int main(){
        foo(){
            printf("bar");
        }
        return 0;
    }

presumably this code would then print out:

    foobar

I understand that this might not be possible by definition, if that's the case I'm sorry for wasting your time.


Solution

  • You probably want to accept a function as parameter, I recommend you to learn how to use c++ lambdas.