Return from function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Return from function

cyboman
Mark, thanks for answering all my questions so far. I really didn't expect such promptness. Here is another one. When we handle the return instruction the return value from the function is placed on the stack (page 163, *ARG = pop() ). How do we handle the case when function doesn't return anything? Are such cases possible on this virtual machine and in the Jack high level language?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Return from function

cadet1620
Administrator
cyboman wrote
Mark, thanks for answering all my questions so far. I really didn't expect such promptness. Here is another one. When we handle the return instruction the return value from the function is placed on the stack (page 163, *ARG = pop() ). How do we handle the case when function doesn't return anything? Are such cases possible on this virtual machine and in the Jack high level language?

Thanks.
As far as the VM is concerned, functions always return a value.

Functions that have no return value (void functions) are defined in the Jack language. For void functions, the compiler generates VM code in the called function to return a 0 on the stack, and the calling function's VM code pops and discards the 0. This behavior is defined in 11.2.1 (top of page 235).

--Mark