Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      return statement

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      Iteration statements
      Jump statements
      return
       

      Terminates current function and returns specified value to the caller function.

      Contents

      [edit]Syntax

      attr-spec-seq(optional)returnexpression; (1)
      attr-spec-seq(optional)return; (2)
      expression - expression used for initializing the return value of the function
      attr-spec-seq -(C23)optional list ofattributes, applied to thereturn statement

      [edit]Explanation

      1) Evaluates theexpression, terminates the current function and returns the result of theexpression to the caller (the value returned becomes the value of the function call expression). Only valid if the function return type is notvoid.
      2) Terminates the current function. Only valid if the function return type isvoid.

      If the type of theexpression is different from the return type of the function, its value isconverted as if by assignment to an object whose type is the return type of the function, except that overlap between object representations is permitted:

      struct s{double i;} f(void);// function returning struct sunion{struct{int f1;struct s f2;} u1;struct{struct s f3;int f4;} u2;} g;struct s f(void){return g.u1.f2;}int main(void){// g.u2.f3 = g.u1.f2; // undefined behavior (overlap in assignment)   g.u2.f3= f();// well-defined}

      If the return type is a real floating type, the result may be represented ingreater range and precision than implied by the new type.

      Reaching the end of a function returningvoid is equivalent toreturn;. Reaching the end of any other value-returning function is undefined behavior if the result of the function is used in an expression (it is allowed to discard such return value). Formain, seemain function.

      Executing thereturn statement in ano-return function is undefined behavior.

      (since C11)

      [edit]Keywords

      return

      [edit]Example

      This section is incomplete
      Reason: improve
      Run this code
      #include <stdio.h> void fa(int i){if(i==2)return;printf("fa():   %d\n", i);}// implied return; int fb(int i){if(i>4)return4;printf("fb():   %d\n", i);return2;} int main(void){    fa(2);    fa(1);int i= fb(5);// the return value 4 used to initializes i    i= fb(i);// the return value 2 used as rhs of assignmentprintf("main(): %d\n", i);}

      Output:

      fa():   1fb():   4main(): 2

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.6.4 The return statement (p: 111-112)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.6.4 The return statement (p: 154)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.6.4 The return statement (p: 139)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.6.4 The return statement

      [edit]See also

      C++ documentation forreturn statement
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/return&oldid=130665"

      [8]ページ先頭

      ©2009-2025 Movatter.jp