简体   繁体   中英

the return statement of C in a function

I am a newbie of C language, I have a question about return statement in C:

void verifyValue(int value)
{
   return;
}

void handleValue(int value)
{
   switch(value)
   {
      case 1:
         // do something
         break;

      case 10:
         verifyValue(value);
         // the rest of code part 1
         break;
      default:
         break;           
   }
}

int main()
{
   int vlaue = 10;
   handleValue(value);

   // the rest of code part 2
}

so the verifyValue() function will return in case 10 , once it returns, will the rest of code part 1 continue to execute or the rest of code part 2 continue to execute, from where this return in verifyValue() really returned?

case 10:
         verifyValue(value);
         // the rest of code part 1
         break;

verifyValue() function is called and after returning from that function

// the rest of code part 1

is executed. After that break is executed so you get out of switch construct.

Later the control is returned to main() and

// the rest of code part 2

is executed.

After verifyValue() call the break statement will work first which will bring the control outside the switch case. after that the handleValue function will return and then "the rest of code part 2" will continue.

The return statement only returns from the function in which it's executed. So this function:

void verifyValue(int value)
{
    return;
}

doesn't do anything. It just immediately returns. Invoking that function has no effect on the logic flow of the code.

"The rest of code part 1" will execute. Then the handleValue() function will implicitly return when it reaches the end of the function. Then "the rest of code part 2" will execute.

return 语句导致被调用函数立即终止,因此,verifyvalue 函数中的 return 语句终止了该函数,并没有结束句柄值函数。在该点之后继续执行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM