My C++ Questions

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 3009 Views - Last Post: 18 November 2010 - 10:31 AM Rate Topic: -----

#1 Dark_Necros  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 253
  • Joined: 29-May 09

My C++ Questions

Posted 20 October 2010 - 03:53 PM

I will frequently be asking some questions about how a function can be used, or nono's about programming in C++, and good habits. My questions will be in spoiler tags to save space, because I think I will be asking quite a few questions. PLEASE POST GOOD HABITS THAT I NEED TO GET INTO!!! I don't want or need to get into bad habits, and have to relearn stuff to make it right.

Question One: The Return Function.

I was going through a tutorial, and I noticed, at first, that there was no return 0; function. In the next example, I noticed that the function was return 1; instead. I am used to seeing the return 0; function. Does this show that it is optional to use this function?

This post has been edited by JackOfAllTrades: 22 October 2010 - 03:49 AM
Reason for edit:: Removed spoiler tags


Is This A Good Question/Topic? 0
  • +

Replies To: My C++ Questions

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: My C++ Questions

Posted 20 October 2010 - 04:01 PM

Return 1; tells the os there was an error and return 0; tells the os that the program exited without any errors.


I hope that helps.

This post has been edited by Alex6788: 20 October 2010 - 04:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dark_Necros  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 253
  • Joined: 29-May 09

Re: My C++ Questions

Posted 20 October 2010 - 04:14 PM

well, the code that I saw (it was the code in the tutorial) had the return 1; function. I am probably not understanding what you are saying, but the tutorial is here and the two peices of code I am talking about are the first two code boxes on the page.
Was This Post Helpful? 0
  • +
  • -

#4 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: My C++ Questions

Posted 20 October 2010 - 04:20 PM

Quote

Upon reaching the end of main, the closing brace, our program will return the value of 0 (and integer, hence why we told main to return an int) to the operating system. This return value is important as it can be used to tell the OS whether our program succeeded or not. A return value of 0 means success and is returned automatically (but only for main, other functions require you to manually return a value), but if we wanted to return something else, such as 1, we would have to do it with a return statement:

#include <iostream>

using namespace std;

int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();

return 1;
}

Quoted from the website.
Was This Post Helpful? 1
  • +
  • -

#5 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: My C++ Questions

Posted 20 October 2010 - 04:21 PM

Taken directly from the page you're refering to, just above the "return 1" example:

Quote

A return value of 0 means success and is returned automatically (but only for main, other functions require you to manually return a value), but if we wanted to return something else, such as 1, we would have to do it with a return statement:


Basically answers your question, doesn't it? And yea this is exactly what Alex6788 was saying

Edit: I can see I wasn't fast enough!

This post has been edited by MathiasVP: 20 October 2010 - 04:21 PM

Was This Post Helpful? 1
  • +
  • -

#6 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: My C++ Questions

Posted 20 October 2010 - 04:23 PM

Quote

I will frequently be asking some questions about how a function can be used, or nono's about programming in C++, and good habits. My questions will be in spoiler tags to save space, because I think I will be asking quite a few questions. PLEASE POST GOOD HABITS THAT I NEED TO GET INTO!!! I don't want or need to get into bad habits, and have to relearn stuff to make it right.

I think NOT posting questions in spolier tags is a good habit you need to get into. Seriously, what makes you think this is a good idea?

As to your question, the answer is on the page you linked to:

Quote

A return value of 0 means success and is returned automatically (but only for main, other functions require you to manually return a value), but if we wanted to return something else, such as 1, we would have to do it with a return statement

Was This Post Helpful? 4
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: My C++ Questions

Posted 20 October 2010 - 05:47 PM

View Postmoopet, on 21 October 2010 - 08:23 AM, said:

I think NOT posting questions in spolier tags is a good habit you need to get into. Seriously, what makes you think this is a good idea?


Completely agree.
It's probably the most annoying choice you could make. Part of the reason I ignored your question when I first read it.

Some reading on return values for main() - follow the links on the page
http://www.gidnetwork.com/b-66.html

This post has been edited by janotte: 20 October 2010 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: My C++ Questions

Posted 20 October 2010 - 06:52 PM

The value returned to the system by the main function is known as exit status. You can use this value to inform something, usually it's use just 0 and 1 that means success and failure respectively.

I did know that there is some kind of standard when returning a value. Searching up I found some references of sysexits.h. I got it right here on my system:
/*
 * Copyright (c) 1987, 1993
 *    The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *    @(#)sysexits.h    8.1 (Berkeley) 6/2/93
 */

#ifndef    _SYSEXITS_H
#define    _SYSEXITS_H 1

/*
 *  SYSEXITS.H -- Exit status codes for system programs.
 *
 *    This include file attempts to categorize possible error
 *    exit statuses for system programs, notably delivermail
 *    and the Berkeley network.
 *
 *    Error numbers begin at EX__BASE to reduce the possibility of
 *    clashing with other exit statuses that random programs may
 *    already return.  The meaning of the codes is approximately
 *    as follows:
 *
 *    EX_USAGE -- The command was used incorrectly, e.g., with
 *        the wrong number of arguments, a bad flag, a bad
 *        syntax in a parameter, or whatever.
 *    EX_DATAERR -- The input data was incorrect in some way.
 *        This should only be used for user's data & not
 *        system files.
 *    EX_NOINPUT -- An input file (not a system file) did not
 *        exist or was not readable.  This could also include
 *        errors like "No message" to a mailer (if it cared
 *        to catch it).
 *    EX_NOUSER -- The user specified did not exist.  This might
 *        be used for mail addresses or remote logins.
 *    EX_NOHOST -- The host specified did not exist.  This is used
 *        in mail addresses or network requests.
 *    EX_UNAVAILABLE -- A service is unavailable.  This can occur
 *        if a support program or file does not exist.  This
 *        can also be used as a catchall message when something
 *        you wanted to do doesn't work, but you don't know
 *        why.
 *    EX_SOFTWARE -- An internal software error has been detected.
 *        This should be limited to non-operating system related
 *        errors as possible.
 *    EX_OSERR -- An operating system error has been detected.
 *        This is intended to be used for such things as "cannot
 *        fork", "cannot create pipe", or the like.  It includes
 *        things like getuid returning a user that does not
 *        exist in the passwd file.
 *    EX_OSFILE -- Some system file (e.g., /etc/passwd, /etc/utmp,
 *        etc.) does not exist, cannot be opened, or has some
 *        sort of error (e.g., syntax error).
 *    EX_CANTCREAT -- A (user specified) output file cannot be
 *        created.
 *    EX_IOERR -- An error occurred while doing I/O on some file.
 *    EX_TEMPFAIL -- temporary failure, indicating something that
 *        is not really an error.  In sendmail, this means
 *        that a mailer (e.g.) could not create a connection,
 *        and the request should be reattempted later.
 *    EX_PROTOCOL -- the remote system returned something that
 *        was "not possible" during a protocol exchange.
 *    EX_NOPERM -- You did not have sufficient permission to
 *        perform the operation.  This is not intended for
 *        file system problems, which should use NOINPUT or
 *        CANTCREAT, but rather for higher level permissions.
 */

#define EX_OK        0    /* successful termination */

#define EX__BASE    64    /* base value for error messages */

#define EX_USAGE    64    /* command line usage error */
#define EX_DATAERR    65    /* data format error */
#define EX_NOINPUT    66    /* cannot open input */
#define EX_NOUSER    67    /* addressee unknown */
#define EX_NOHOST    68    /* host name unknown */
#define EX_UNAVAILABLE    69    /* service unavailable */
#define EX_SOFTWARE    70    /* internal software error */
#define EX_OSERR    71    /* system error (e.g., can't fork) */
#define EX_OSFILE    72    /* critical OS file missing */
#define EX_CANTCREAT    73    /* can't create (user) output file */
#define EX_IOERR    74    /* input/output error */
#define EX_TEMPFAIL    75    /* temp failure; user is invited to retry */
#define EX_PROTOCOL    76    /* remote error in protocol */
#define EX_NOPERM    77    /* permission denied */
#define EX_CONFIG    78    /* configuration error */

#define EX__MAX    78    /* maximum listed value */

#endif /* sysexits.h */

Was This Post Helpful? 2
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5348
  • View blog
  • Posts: 27,305
  • Joined: 10-May 07

Re: My C++ Questions

Posted 20 October 2010 - 07:03 PM

Return codes pass to the calling function, not just the Operating System. Example
Was This Post Helpful? 0
  • +
  • -

#10 Dark_Necros  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 253
  • Joined: 29-May 09

Re: My C++ Questions

Posted 21 October 2010 - 08:23 PM

I think I just need to learn some more terminology... Why cant I edit my previous posts??? I was going to edit the Spoiler Tags out, but there isn't the edit option in the usual spot...

This post has been edited by Dark_Necros: 21 October 2010 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#11 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: My C++ Questions

Posted 21 October 2010 - 08:26 PM

After a few days the edit button goes away.

Did you see my posts, do you understand it now.

This post has been edited by Alex6788: 21 October 2010 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#12 Dark_Necros  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 253
  • Joined: 29-May 09

Re: My C++ Questions

Posted 22 October 2010 - 10:37 AM

hm... I get that you all are trying to tell me that it is extremely simple, but sometimes... Simple is hard for me to understand lol.
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: My C++ Questions

Posted 22 October 2010 - 11:00 AM

Quote

Question One: The Return Function.


Return is a statement and not a function.

There is a big difference between the two things.
A function represents a subroutine that preforms some calculation and give some value back to the calling procedure.
A statement refers to an element of a language's syntax such as if-statements, else-statements, switch-statements etc.

The return statement is used within a function to specify the value that will be passed back to the calling procedure.


Lets say for example I would like to make a function that will add two numbers together:

#include <iostream>

using namespace std;

int add(int a, int B)/> { return a + b; }

int main() {
    int result = add(10, 5);
    cout << result << endl;

    return 0;
}


The statement: return a + b tells the compiler that the calculated value of the expression a + b should be returned as the value of the function.

Think back to math class where you might have f(x) = x^2 in a C/C++ this would be:

[code] float f(float x) { return x * x; }[code]

so the line f(5) would then evaluate to 25 because that is the value "returned" by the function.


in C/C++ the main entry point of the program is generally main() -- this is a FUNCTION and therefore returns a value. The OS or calling process can use this return value. As mentioned before it is generally the convention that 0 means there were no errors and a non-zero value means there is an error. HOWEVER you can use this return value for other things.

For example the choice.exe program that aids in writing batch files by presenting a user with a small menu and then returning a value to indicate which menu item was picked. So here the return value of main() is determined by what menu item the user picked.

Some programs (especially old DOS programs) have long lists of error codes and what they meant -- in modern terms this feature is not used terribly often though it does have its uses.
Was This Post Helpful? 2
  • +
  • -

#14 moosa_010  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 3
  • Joined: 22-October 10

Re: My C++ Questions

Posted 22 October 2010 - 08:49 PM

when I begin with coding c .I remember I ask my teacher about the return value in the main function.

in main don't care if u return 0 or 1 , I prefer using void in main function.
Was This Post Helpful? -4
  • +
  • -

#15 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: My C++ Questions

Posted 22 October 2010 - 11:28 PM

View Postmoosa_010, on 22 October 2010 - 09:49 PM, said:

when I begin with coding c .I remember I ask my teacher about the return value in the main function.

in main don't care if u return 0 or 1 , I prefer using void in main function.

Are you saying you prefer using void main() over int main()?
If yes then that's not the standard way, int main() is the standard.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3