Programming

    Basics about programming

    Stackful vs Stackless Coroutine

    A coroutine is a function that can be paused and resumed later. Two main principles exists to save the states: stackless and stackful.

    In contrast to a stackless coroutine a stackful coroutine can be suspended from within a nested stackframe. Execution resumes at exactly the same point in the code where it was suspended before. With a stackless coroutine, only the top-level routine may be suspended. Any routine called by that top-level routine may not itself suspend. This prohibits providing suspend/resume operations in routines within a general-purpose library.

    source

    To boil it down to a very simplistic definition, a stackful coroutine model would let you suspend or yield from anywhere. A stackless model is restricted to suspending only from inside coroutines, but the benefit is that it is lighter weight. Also, it's probably more feasible to retrofit to an existing language.

    source 2

    See also technical explanation

    Callback

    Callback((fonction de rappel en francais)) is a function passing by parameter of another function.

    See this example from wikipedia:

    #include `<stdio.h>`
    #include `<stdlib.h>`
    
    /* The calling function takes a single callback as a parameter. */
    void PrintTwoNumbers(int (*numberSource)(void)) {
        printf("%d and %d\n", numberSource(), numberSource());
    }
    
    /* A possible callback */
    int overNineThousand(void) {
        return (rand() % 1000) + 9001;
    }
    
    /* Another possible callback. */
    int meaningOfLife(void) {
        return 42;
    }
    
    /* Here we call PrintTwoNumbers() with three different callbacks. */
    int main(void) {
        PrintTwoNumbers(&rand);
        PrintTwoNumbers(&overNineThousand);
        PrintTwoNumbers(&meaningOfLife);
        return 0;
    }
    

    Loop

    Pass one time the loop:

    #include `<stdio.h>`
    
    int main(){
     int i=0;
     for(; i<4; i++)
     {
      if(i==2){
       continue; // NOTE this instruction
      }
    
      printf("%d, ", i);
     }
    
     return 0;
    }
    

    Output: 0, 1, 3,

    Pass all loop:

    #include `<stdio.h>`
    
    int main(){
     int i=0;
     for(; i<4; i++)
     {
      if(i==2){
       break; // NOTE this instruction
      }
    
      printf("%d, ", i);
     }
    
     return 0;
    }
    

    Output: 0, 1,