Difference between revisions of "SHIP:Sail:Flow Control:return"

From Serious Documentation
Jump to: navigation, search
 
Line 12: Line 12:
 
! scope="col" style="text-align:left" | Description
 
! scope="col" style="text-align:left" | Description
 
|-
 
|-
|[[SHIP:Sail:Flow_Control|return]]||Causes immediate termination of a function.  Can pass a value back to the caller when a value is placed as its argument.
+
|[[SHIP:Sail:Flow_Control|return]]||Causes immediate termination of a function and passes control back to the caller.
 
|}</onlyinclude>
 
|}</onlyinclude>
 
== Example ==
 
== Example ==
 
<source lang="c">
 
<source lang="c">
int foo_1()
+
switch(a)
 
{
 
{
   a = 10;
+
   case 1:
  while(a)
+
      printf("1");
  {
+
  case 2:
      if(a == 5)
+
      printf("2");
        return a;
+
  case 3:
  }
+
      printf("3");
}</source>
+
      break;
 
+
  case 4:
or
+
      printf("4");
 
+
  default;
<source lang="c">
+
}
foo_2()
+
return;</source>
{
 
  switch(a)
 
  {
 
      case 1:
 
        printf("1");
 
      case 2:
 
        printf("2");
 
      case 3:
 
        printf("3");
 
        break;
 
      case 4:
 
        printf("4");
 
      default;
 
  }
 
  return;
 
}</source>
 
 
Prints:
 
Prints:
a = 1 ->123
+
a = 1 ->123,
a = 2 ->23
+
a = 2 ->23,
a = 3 ->3
+
a = 3 ->3,
 
a = 4 ->4
 
a = 4 ->4
  
 
[[Category:Flow Control]]
 
[[Category:Flow Control]]

Latest revision as of 17:33, 1 November 2016

See Also

return

Causes execution to exit a function or program and go back to caller.

Statement Description
return Causes immediate termination of a function and passes control back to the caller.

Example

switch(a)
{
   case 1:
      printf("1");
   case 2:
      printf("2");
   case 3:
      printf("3");
      break;
   case 4:
      printf("4");
   default;
}
return;

Prints: a = 1 ->123, a = 2 ->23, a = 3 ->3, a = 4 ->4