Like Tree2Likes
  • 2 Post By Elysia

some basic questions about C++

This is a discussion on some basic questions about C++ within the C++ Programming forums, part of the General Programming Boards category; Hi Please help me with the following queries to understand C++ conceptually. It would be really nice of you if ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    some basic questions about C++

    Hi

    Please help me with the following queries to understand C++ conceptually. It would be really nice of you if you keep your explanation simple so that I can understand it.

    1: What is a 'library function'? Is it the function included in the standard C++ library - i.e. the header #include <cstdlib>?

    2: What is the difference between a function and function call?

    3: Can I say the function "int main()" is the 'mother' function and all (or, most others) are its children functions such as sqrt()?

    4: The book says:
    Library Function exit()
    This function causes the program to terminate, no matter where it is in the listing.
    It has no return value. Its single argument, 0 in our example, is returned to the operating system when the program exits. (This value is useful in batch files, where you can use the ERRORLEVEL value to query the return value provided by exit(). The value 0 is normally used for a successful termination; other numbers indicate errors.)


    What is "return value"? So far I have only seen int main() with which "return 0;" is used, and that too I have been told is optional.

    Using exit(0), exit(1), exit(2),..., doesn't affect the working of the below given code but the above passage says only "0" value is used for successful termination.

    5: The book says:
    The last statement in the function body is return 0;. This tells main() to return the value 0 to whoever called it, in this case the operating system or compiler. In older versions of C++ you could give main() the return type of void and dispense with the return statement, but this is not considered correct in Standard C++.

    I don't understand it. What is "void"?

    Is return 0; a kind of signal at the end or somewhere else to tell that the job has been done and it's time to call it a day. Suppose if you have return 0; at the end of the code then the return 0; statement would only be reached when the execution of the code has successfully executed the above lying statements and once reached it tells job is done.

    Code:
        #include <iostream>
        #include <cstdlib>
    
        using namespace std;
    
        int main()
    
        {
            int n, i;
            bool prime = true;
    
            cout << "Enter the number: ";
            cin >> n;
    
            if (n == 1)
            {
            cout << "1 is neither considered a prime nor non-prime" << endl;
            exit(0);
            }
    
            for (i=2; i<=(n/2);i++)
    
            {
                if (n%i != 0)
                prime = true;
    
    
                else if (n%i == 0)
                prime = false;
    
                {
                    if (prime == false)
                    break;
                }
            }
    
            if (prime == true)
            cout << "the number is prime" << endl;
    
            else
            cout << "not prime" << endl;
    
            system("pause");
        }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Quote Originally Posted by jackson6612 View Post
    2: What is the difference between a function and function call?
    Code:
    void foo() { }
    foo();
    foo is a function, and foo(); is a function call.

    3: Can I say the function "int main()" is the 'mother' function and all (or, most others) are its children functions such as sqrt()?
    What is your definition of "mother function"? What does it mean?

    What is "return value"? So far I have only seen int main() with which "return 0;" is used, and that too I have been told is optional.
    Consider:
    Code:
    int foo() { return 5; }
    int main()
    {
    	int ret = foo();
    }
    The return value of foo is what foo returns, in this case. In main, the variable ret will hold the return value for foo.

    Using exit(0), exit(1), exit(2),..., doesn't affect the working of the below given code but the above passage says only "0" value is used for successful termination.
    exit is simply a function that terminates your program. Typically, your program will end when the main function ends, but you can force it to end before (typically not recommended, however).
    Whatever you send as argument to exit will be the return value of the program. 0 is typically used to indicate success, but there is no standard, so it can be whatever you mean it to be.
    The return value of a program is basically some information some other program can acquire to know the status of success of an executed program.

    I don't understand it. What is "void"?
    void is a return type, just as int.
    Example:
    Code:
    void foo1();
    int foo2();
    foo1 returns nothing (hence void).
    foo2 returns an int.

    Is return 0; a kind of signal at the end or somewhere else to tell that the job has been done and it's time to call it a day. Suppose if you have return 0; at the end of the code then the return 0; statement would only be reached when the execution of the code has successfully executed the above lying statements and once reached it tells job is done.
    No. A return statement simply says that the currently executing function shall end and it may return a value.
    The caller of this function can then store whatever this function returned.
    0 is not a special number; it can be anything you want and you must decide on how to interpret it.
    r.hackett and jackson6612 like this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Elysia. It was very helpful indeed. Though I do have some follow-on questions, I think it's better to first digest well the material I have been given in your previous post. Once again, thanks.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    Quote Originally Posted by jackson6612 View Post
    3: Can I say the function "int main()" is the 'mother' function and all (or, most others) are its children functions such as sqrt()?
    You should say that main is the entry point, since that is true unless there are (user defined) global objects. An entry point is where a program begins executing, which is probably what you mean. If there are global objects though, then technically the first one defined would be the entry point. Other functions would be children of main if main calls them. If you use parent and children while talking to someone about functions, you might get an odd response: parent and children usually refer to processes, instead. A parent process spawns a child process.
    Last edited by whiteflags; 04-30-2011 at 12:15 PM.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by jackson6612 View Post
    1: What is a 'library function'? Is it the function included in the standard C++ library - i.e. the header #include <cstdlib>?
    For someone who isn't working on a team of programmers, a library function is pretty much anything you call that you didn't write yourself.

    2: What is the difference between a function and function call?
    What's the difference between a car, and driving a car? Same thing.

    3: Can I say the function "int main()" is the 'mother' function and all (or, most others) are its children functions such as sqrt()?
    You can say anything you like. If you want to talk about a call graph which depicts the caller/callee relationships between functions, then yeah, you can think of main() as a parent and sqrt() as a child, but it depends on the code.

    I don't understand it. What is "void"?
    void is nothing. In certain places in the language, a type name is required. "void" is the type name you use when you mean "nothing." This is unrelated to "void *" which means a pointer to an unspecified type, not "pointer to nothing."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic questions about C
    By -EquinoX- in forum C Programming
    Replies: 6
    Last Post: 03-13-2008, 03:59 AM
  2. hello. basic questions
    By johnnyboy in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2008, 09:58 AM
  3. 2 Basic Questions
    By Dan99 in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2007, 06:11 PM
  4. Basic questions about C
    By FlatLost in forum C Programming
    Replies: 6
    Last Post: 11-01-2005, 12:08 PM
  5. basic questions on C#
    By black in forum C# Programming
    Replies: 3
    Last Post: 12-09-2003, 08:23 AM

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