Return values

This is a discussion on Return values within the C++ Programming forums, part of the General Programming Boards category; Can a function that dosnt return values, have a return statement.? What is it's effect?...

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Return values

    Can a function that dosnt return values, have a return statement.? What is it's effect?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Some compilers will let you have an empty return statement. Those statements end the function but do not return anything. Not all compilers will accept those statements so do not use them.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    All compilers should accept an empty return statement with void functions:
    Code:
    void function() {
      return; // this is always okay
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,459
    What is it's effect?
    the function execution stops and control returns to the calling function

    Code:
    void print_string(char* string)
    {
       if(string == NULL)
          return; /* we do not want continue if the null-pointer was put as a parameter */
    
       /* do something useful with the string */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Desolation
    Some compilers will let you have an empty return statement. Those statements end the function but do not return anything. Not all compilers will accept those statements so do not use them.
    Which horribly broken compilers will not allow early returns from void functions?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Sometimes you want to return from a void function earlier than the closing brace, in which case the compiler had better support it.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by CornedBee
    Which horribly broken compilers will not allow early returns from void functions?
    MSVC++ 7.3. I can't do that.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What is 7.3? Do you mean 2003 (7.1)?

    I think you might be confused though. You can do it just fine in VC++ 6.0 and higher (and probably in previous versions as well). Also, we are talking about C++ here, not C.
    Code:
    #include <iostream>
    
    void output_if_positive(int value)
    {
      if (value <= 0)
        return;
    
      std::cout << value << '\n';
    }
    
    int main()
    {
      output_if_positive(-2);
      output_if_positive(5);
    }

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    Maybe I confused the IDEs, but I am 100&#37; positive that I have tried an empty return statement in a compiler and have got an error because of it.

  10. #10
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I'm 100&#37; positive the error was something else and you confused it with an empty return in a void function. Maybe an empty return in a non-void function, or a non-empty return in a void function, but an empty return in a void function has always been legal C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21