📜  C / C++中的带有示例的return语句

📅  最后修改于: 2021-05-26 03:28:55             🧑  作者: Mango

先决条件: C / C++中的函数

return语句将执行流从调用处返回给函数。该语句不需要强制性的任何条件语句。一旦执行该语句,程序流将立即停止,并从调用它的位置返回控件。 return语句可能会或可能不会为void函数返回任何内容,但对于非void函数,必须返回一个返回值。

句法:

return[expression];


有多种使用return语句的方法。下面很少提到:

  1. 不返回值的方法:在C / C++中,当方法为返回类型时,不能跳过return语句。仅对于void类型,可以跳过return语句。
    • 在void return type 函数不使用return语句:当函数不返回任何内容时,将使用void return类型。所以,如果有在函数定义返回类型为void,那么就会出现该函数(一般)内部没有return语句。

      句法:

      void func()
      {
          .
          .
          .
      }
      

      例子:

      C
      // C code to show not using return
      // statement in void return type function
        
      #include 
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
        
          return 0;
      }


      C++
      // C++ code to show not using return
      // statement in void return type function
        
      #include 
      using namespace std;
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
        
          return 0;
      }


      C
      // C code to show using return
      // statement in void return type function
        
      #include 
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return statement
          return;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
          return 0;
      }


      C++
      // C++ code to show using return
      // statement in void return type function
        
      #include 
      using namespace std;
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return statement
          return;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
          return 0;
      }


      C
      // C code to show using return statement
      // with a value in void return type function
        
      #include 
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return
          // statement to return a value
          return 10;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
          return 0;
      }


      C++
      // C++ code to show using return statement
      // with a value in void return type function
        
      #include 
      using namespace std;
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return
          // statement to return a value
          return 10;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
        
          return 0;
      }


      C
      // C code to illustrate Methods returning
      // a value using return statement
        
      #include 
        
      // non-void return type
      // function to calculate sum
      int SUM(int a, int b)
      {
          int s1 = a + b;
        
          // method using the return
          // statement to return a value
          return s1;
      }
        
      // Driver method
      int main()
      {
          int num1 = 10;
          int num2 = 10;
          int sum_of = SUM(num1, num2);
          printf("The sum is %d", sum_of);
          return 0;
      }


      C++
      // C++ code to illustrate Methods returning
      // a value using return statement
        
      #include 
      using namespace std;
        
      // non-void return type
      // function to calculate sum
      int SUM(int a, int b)
      {
          int s1 = a + b;
        
          // method using the return
          // statement to return a value
          return s1;
      }
        
      // Driver method
      int main()
      {
          int num1 = 10;
          int num2 = 10;
          int sum_of = SUM(num1, num2);
          cout << "The sum is " << sum_of;
          return 0;
      }


      输出:
      Welcome to GeeksforGeeks
      
    • 在void return type 函数使用return语句:现在出现了问题,如果void return type函数有return语句,该怎么办?因为我们知道,如果在函数定义返回类型为void,那么就会出现该函数内部没有return语句。但是,如果其中包含return语句,则语法也将是没有问题的:

      正确的语法:

      void func()
      {
          return;
      }
      

      此语法在函数用作跳转语句,以中断函数流并从中跳出。可以将其视为函数中使用的“ break语句”的替代方法。

      例子:

      C

      // C code to show using return
      // statement in void return type function
        
      #include 
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return statement
          return;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
          return 0;
      }
      

      C++

      // C++ code to show using return
      // statement in void return type function
        
      #include 
      using namespace std;
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return statement
          return;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
          return 0;
      }
      
      输出:
      Welcome to GeeksforGeeks
      

      但是,如果return语句尝试在void return type函数返回值,则将导致错误。

      语法错误:

      void func()
      {
          return value;
      }
      

      警告:

      warning: 'return' with a value, in function returning void
      

      例子:

      C

      // C code to show using return statement
      // with a value in void return type function
        
      #include 
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return
          // statement to return a value
          return 10;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
          return 0;
      }
      

      C++

      // C++ code to show using return statement
      // with a value in void return type function
        
      #include 
      using namespace std;
        
      // void method
      void Print()
      {
          printf("Welcome to GeeksforGeeks");
        
          // void method using the return
          // statement to return a value
          return 10;
      }
        
      // Driver method
      int main()
      {
        
          // Calling print
          Print();
        
          return 0;
      }
      
      警告:
      prog.c: In function 'Print':
      prog.c:12:9: warning: 'return' with a value, in function returning void
        return 10;
               ^
      
  2. 返回值的方法对于定义返回类型的方法,必须在return语句后紧跟该指定返回类型的返回值。

    句法:

    return-type func()
    {
        return value;
    }
    

    例子:

    C

    // C code to illustrate Methods returning
    // a value using return statement
      
    #include 
      
    // non-void return type
    // function to calculate sum
    int SUM(int a, int b)
    {
        int s1 = a + b;
      
        // method using the return
        // statement to return a value
        return s1;
    }
      
    // Driver method
    int main()
    {
        int num1 = 10;
        int num2 = 10;
        int sum_of = SUM(num1, num2);
        printf("The sum is %d", sum_of);
        return 0;
    }
    

    C++

    // C++ code to illustrate Methods returning
    // a value using return statement
      
    #include 
    using namespace std;
      
    // non-void return type
    // function to calculate sum
    int SUM(int a, int b)
    {
        int s1 = a + b;
      
        // method using the return
        // statement to return a value
        return s1;
    }
      
    // Driver method
    int main()
    {
        int num1 = 10;
        int num2 = 10;
        int sum_of = SUM(num1, num2);
        cout << "The sum is " << sum_of;
        return 0;
    }
    
    输出:
    The sum is 20
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”