in Compiler Design recategorized by
2,620 views
12 votes
12 votes
State whether the following statements are True or False with reasons for your answer:

Coroutine is just another name for a subroutine.
in Compiler Design recategorized by
2.6k views

1 comment

Subroutine will always starts its execution from the beginning(first line) but a co-routine will start from where it left last time.

This is why we say, co-routine has multiple entry points whereas sub-routine has only one.

Yield 'remembers' where the co-routine is so when it is called again it will continue where it left off.

For example:

  coroutine foo {
    yield 1;
    yield 2;
    yield 3;
  }
  print foo();
  print foo();
  print foo();

Prints: 1 2 3

Note: Coroutines may use a return, and behave just like a subroutine

  coroutine foo {
    return 1;
    return 2; //Dead code
    return 3;
  }
  print foo();
  print foo();
  print foo();

Prints: 1 1 1

Source - Stack Overflow

6
6

2 Answers

9 votes
9 votes

True. The subroutine is a special case of a co-routine. A co-routine is a generalized form of a subroutine which is non-preemptive multitasking.

https://en.wikipedia.org/wiki/Coroutine

edited by

1 comment

@srestha if coroutines are generalized form then can we say it is just another name of subroutine?It seems from your definition that coroutines can have multiples things and one of them is subroutine.So i think first is false
1
1
3 votes
3 votes

TRUE: A subroutine and a function are essentially the same thing, with one difference: A function returns some sort of value (usually via the stack or CPU register), while a subroutine does not. Whether subroutine or function, it is a block of executable code, having exactly one point of entry. A co-routine is also a block of executable code, and, just like a subroutine, it has one point of entry. However, it also has one or more points of re-entry.

ref: https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwjawbP86t3WAhWLOo8KHXxhCpsQFgg5MAM&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F24780935%2Fdifference-between-subroutine-co-routine-function-and-thread&usg=AOvVaw3yAD5Td224hZer_CQWxfAQ

Answer:

Related questions