Static Semantics: void return

In order to return to the caller function, a void function is allowed to have a return; statement without an expression, isn’t it?

e.g.

int x;
void foo() {
    x = 3;
    return;
}
int main() {
    foo();
    return x;
}

According to the grammar it is valid sine the grammar is ReturnStatement := return Expression? so the expression can appear 0 or 1 time.

with respect to the static semantic, an expression has to be present if and only if the return type is not void page 6, so if the expression is not provided, this rule will not be violated.

hence, it’s valid.

3 Likes