Thread: Declarations

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Unhappy Declarations

    I'm currently reading C++ Primer 6th Ed, and have just finished chapter 2.
    I understand that programming is all about doing things in order, and how cout and cin work. BUT...
    im having serious trouble understanding what these declarations are (void, double, int, pow).

    I know im an amateur, and you experienced people are probably sick of answering these same questions, but i really need help.

    Thank you
    Anthony

    P.S. I am an absolute beginner in programming altogether so if there are any better books, more beginner jargon friendly, that i should be reading to understand terms better, ANY suggestions are more than welcome!

  2. #2

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please post some code that illustrates your problems.

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Code:
    // ourfunc1.cpp -- repositioning the using directive
    Code:
    
    #include <iostream>
    
    using namespace std; // affects all function definitions in this file
    
    void simon(int);
    
    int main()
    
    {
    
    simon(3);
    
    cout << "Pick an integer: ";
    
    int count;
    
    cin >> count;
    
    simon(count);
    
    cout << "Done!" << endl;
    
    return 0;
    
    }
    
    void simon(int n)
    
    {
    
    cout << "Simon says touch your toes " << n << " times." << endl;
    
    }
    


    As you can see, this uses int and void, both of which i dont understand, but there is also the other declarations in the original post that i dont understand. It doesnt really explain what the difference is between void and int, what they do, what return statements are etc.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    All of these ( except pow ) are keywords of built-in types.

    "void" just denotes the absence of type, meaning no return should be made.
    "double" is a floating-point number, capable of integer, fractional and exponential values. It's at least as accurate as "float", but on x86 systems is always at least double that precision.
    "int" is a regular integer or absolute number ( ..., -2, -1, 0, 1, 2, ... ), which is at least 16-bits, but in many modern systems it's 32-bits in size.

    These and other keywords are used to declare variables, constants, functions or cast between types.

    Is that what you're asking?
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Thanks reaper but as i said, total beginner and everything you jost posted was like speaking french to me Im starting to wonder if i should just give up? I dont seem to be getting any of the basic stuff....

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by brucey2343 View Post
    Thanks reaper but as i said, total beginner and everything you jost posted was like speaking french to me Im starting to wonder if i should just give up? I dont seem to be getting any of the basic stuff....
    Now, now, don't despair already. There's plenty of time for that!

    Let's break it down, what don't you understand?
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    absence of type, meaning no return should be made.

    floating-point number, capable of integer, fractional and exponential values

    accurate as "float", but on x86 systems is always at least double that precision.

    at least 16-bits, but in many modern systems it's 32-bits in size (size of what?

    variables, constants, functions or cast between types...

    Yeah, so basically all of it Its something i would really love to do, but i just seems SO complicated with all the jargon. I successfully finished the program challenges in the book, by using the chapter as a reference, but it felt like i was typing what it wanted to see but didnt understand what i was saying to the computer. I know what to do, but dont know why i do them :'(

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    absence of type, meaning no return should be made.
    Functions have types. A type can be anything from int to double to string to an abstract object that you created. These function should ( or must ) return something as a result. In contrast, a function whose type is "void" mustn't return anything, and in fact the compiler will complain if you try to make it so.

    floating-point number, capable of integer, fractional and exponential values
    Ignore the "floating-point" for the moment.
    integer = ( 0, 1, 2, ... ) and their negatives
    fractional = ( 1/2, 1/3, 1/4, 1/5, ... ) and their negatives
    exponential = ( 10¹, 10², 10³, ...) and their negatives, taking 10 as base.
    A "double" can be any possible combination between the above ( if it's precision allows it of course ).

    accurate as "float", but on x86 systems is always at least double that precision.

    at least 16-bits, but in many modern systems it's 32-bits in size (size of what?
    Ignore and ignore, you'll learn of these soon enough.

    variables, constants, functions or cast between types...
    A variable is something that it's value can be changed, a constant is the opposite. A function is a set of "canned" instructions that can be executed, as a separate unit, from anywhere ( well, kinda ). Casting you'll learn about that too as you move on, it basically convert one type into another...

    Phew...
    Last edited by GReaper; 11-13-2012 at 10:58 AM.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Haha, thanks very much, that really helped but i do have one more question.

    You said that void functions (is that right?) can not, must not and will not return anything. If you draw your attention to the code i posted, the second function is a void function, but it returns cout back to the main() function?

    Code:
    void simon(int n)
    Code:
    
    {
    cout << "Simon says touch your toes " << n << " times." << endl;
    
    Code:
    }
    


  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by brucey2343 View Post
    ... but it returns cout back to the main() function?
    It may look like a continuous print in the console, but those inside "main" and in "simon" are different calls. I was talking about returning values, with a special keyword called "return". In fact, your "main" function has one at the very end.
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    i dont understand.... what is return then? I would assume it means to return something to the calling function? I read that main is technically called by the OS, and it returns 0 at the end of main to finish the program?

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by brucey2343 View Post
    I would assume it means to return something to the calling function? I read that main is technically called by the OS, and it returns 0 at the end of main to finish the program?
    Couldn't have said it better.
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Why thank you but then doesnt the void simon function return to the main() function when its finished the cout command?

  15. #15
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by brucey2343 View Post
    Why thank you but then doesnt the void simon function return to the main() function when its finished the cout command?
    It returns the flow of execution, yes, but it doesn't return any value that you could save for later. For example, if "myFunction" returns a value I could do "result = myFunction();", and then do anything I want with "result".
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declarations and definitions in C
    By jack1234 in forum C Programming
    Replies: 8
    Last Post: 12-17-2007, 11:24 AM
  2. Declarations
    By slippy in forum C++ Programming
    Replies: 13
    Last Post: 12-01-2007, 05:02 PM
  3. Function declarations
    By ssharish2005 in forum C Programming
    Replies: 9
    Last Post: 10-09-2007, 07:06 AM
  4. portable declarations
    By Marv in forum C Programming
    Replies: 10
    Last Post: 04-12-2007, 04:39 PM
  5. help on declarations
    By robasc in forum C Programming
    Replies: 9
    Last Post: 03-05-2005, 01:50 PM

Tags for this Thread