-3

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.

  • 4
    I have no idea what you are asking. Wouldn't the body of a function count as a statement block by your definition? Adopting your nomenclature, `void func(){ }`. – Pradhan Feb 19 '15 at 03:58
  • 2
    can you please define "statement block". You can put as many statements as you want in a for loop in c, c++, and every imperative language ever written. – Red Alert Feb 19 '15 at 03:58
  • You *do* know how to define functions? You *do* know that you can place any sequence of statements inside a `{}` block? – Some programmer dude Feb 19 '15 at 03:59
  • @Etaoin Shrdlu .Your answer here : http://stackoverflow.com/questions/2929281/are-nested-functions-a-bad-thing-in-gcc – MONTS_MIND_Hacker Feb 19 '15 at 04:00
  • well any code really, so something you can put in a for loop... I'm sorry if I don't know how to express myself better, but for instance, how would you define the for function if it didn't exist in C? – Etaoin Shrdlu Feb 19 '15 at 04:00
  • `for` isn't a *function*, it's a *statement* and is built into the language, same a `if` or `while`. You can't create new statements, hardly any language allows that. I think you need to go read a good *and basic* book on C ([or C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)). – Some programmer dude Feb 19 '15 at 04:01
  • @JoachimPileborg thank you! this is exactly what I was asking for. so it isn't possible to define such a thing, that's all I was asking for. – Etaoin Shrdlu Feb 19 '15 at 04:03
  • @EtaoinShrdlu You could express it recursively assuming function calls are allowed. – Pradhan Feb 19 '15 at 04:03
  • if anyone is still interested I've edited the question to try and be more specific, though Joachim pretty much settled it – Etaoin Shrdlu Feb 19 '15 at 04:11
  • 1
    This hypothetical program in your example - what is it supposed to print when run? – Igor Tandetnik Feb 19 '15 at 04:11
  • @IgorTandetnik I've added that. though I've never seen such a thing used in any program and that's what made me interested in the whole thing, so I really don't know how it would be handled by the language – Etaoin Shrdlu Feb 19 '15 at 04:14
  • How is this supposed to be different from plain old `int main() { foo(); printf("bar"); }` ? – Igor Tandetnik Feb 19 '15 at 04:15
  • this really wasn't the point of the example, but to clarify what exactly it is I wanted to do... I'd want to handle `printf("bar");` inside the `foo()` function somehow – Etaoin Shrdlu Feb 19 '15 at 04:16
  • if this still doesn't make sense, consider a makeshift for function `int for(**args){/*do what for does*/ return 0;} int main(){for(){printf('foo');}return 0;}` – Etaoin Shrdlu Feb 19 '15 at 04:20
  • Read about algorithms (e.g. `std::for_each`), function pointers, and lambdas in your favorite C++ textbook. – Igor Tandetnik Feb 19 '15 at 04:21
  • I think it's pretty obvious he's asking about lambda functions. @Etaoin, in C++ it's something like `foo([](){ printf("meep"); });` – Blindy Feb 19 '15 at 04:22
  • @Blindy I'm familiar with lambdas from python (in fact I use them quite a lot there) and haskell, are these similar? I'm asking because in python these are just "mini functions" that usually do some menial task – Etaoin Shrdlu Feb 19 '15 at 04:25
  • Isn't that what you want, to embed caller-side functions inside other functions at predetermined locations? That's lambdas. Or are you looking more for injection (ie forced injection against the callee's will)? You can't really do that in plain C++, that's more of a managed language thing (C#, Java etc). – Blindy Feb 19 '15 at 04:35
  • it is that technically, I was just wondering if it was possible to get the syntax a for or if or while statement has. func(){} that is all – Etaoin Shrdlu Feb 19 '15 at 05:02
  • Statement blocks are syntactic elements that exist in other languages. C/C++ does not support it to my knowledge. – Juergen Feb 19 '15 at 05:06

1 Answers1

0

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

oblitum
  • 11,380
  • 6
  • 54
  • 120