Thread: Need some help with the exercise from the book

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    Need some help with the exercise from the book

    Well...here is the exercise. This is the question from the tutorial book.

    Write a function named print_out that prints all the whole numbers from 1 to N. Test the function by placing it in a program that passes a number n to it, where this number is entered from the keyboard. The print_out function should have type void; it does not return a value. The function can be called with a simple statement:

    print_out(n);

    Well...My question is how it is possible to do it without return a value because we need to get something from the user and the whole number should be integer. Without returning a value, how can we do that?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read it again: you've already got the number n from the user before you call the function; how else do you pass it in? And what value would you plan to return?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lets make this more visual for you (cuz sometimes it helped me):

    Make a function named print_out(whole_number_type n).

    Now it the program needs to do take the user input and pass it to your function.

    Your function should not return anything and will be called like: print_out(n);

    ---------
    I am not trying to insult your intelligence I am thinking maybe the concept was escaping you a bit.

    So question number one is what type works with whole numbers?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    This is my code. But it does not compile and states that there are too many arguments. What have I done wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    void print_out();
    
    int main()
    {
        int n;
        cout<< "Enter a number: ";
        cin>> n;
        print_out(n);
    
    return 0;
    }
    
    void print_out (int n)
    {
        int i;
        for(i = 1; i <= n; i++)
            cout<< i << endl;
    
    }

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    15
    it looks like your function prototype is missing an argument. try this:

    Code:
    void print_out(int);

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In C, that prototype would be sufficient, by the way. In C++ it is interpreted as void print_out(void). Its one important difference between the languages that you should note. Though given your initial quesiton I believe its safe to assume you are not a C programmer to begin with.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Quote Originally Posted by master5001 View Post
    In C, that prototype would be sufficient, by the way. In C++ it is interpreted as void print_out(void). Its one important difference between the languages that you should note. Though given your initial quesiton I believe its safe to assume you are not a C programmer to begin with.
    True. I used to learn Java, and I found it extremely difficult and as for C, I have no experience of that.

    Besides this, I have a few questions I'd like to ask. These questions have been confusion to me.

    1) Do we always have to declare the function in the very beginning the moment we start the program after the initial library has been declared such as #include directive? What's the benefit and why?

    2) What is the difference between declaration and prototype? parameter and argument_list?

    3) As for the void type, though asking this would sound stupid, How do we know when to use it? How does it benefit the program?

    4) As for parameter or argument_list, how do we know when do we need to include the types or names? when should we leave it empty?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. A function must have a valid declaration or definition before it is used. Most headers just, in fact, contain declarations of functions.
    2. A prototype is a function declaration. It just says "this is a function name, these are the parameters, this is the return type, and we'll figure the rest out later". You can also declare a function by defining it (giving the body of the function).
    3. A function that you don't want to return anything returns nothing ("void").
    4. You always always always always always always always always always include the types of parameters in the function prototype and definition. You should include names, but you don't have to if you don't want to for some strange reason. You can only leave it empty if that function does not take any arguments.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Would you please list a few examples? because it really helps when it comes to something confusing esp a newbie like me. I still don't get it what the difference is between parameter and arguments. And when we should include them and when you should not. For instance, the void function if print the message doesn't require the argument because it just prints the message but sometimes it does include just like the exercise above void variable (int). This is what I wanna know when it comes to value or something else.

    Furthermore, you said you could declare a function by defining it...Would you also give me an example of it?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The above example, your post #4, is not correct. void print_out() and void print_out(int n) are not compatible (you could get away with it in C, but not C++).

    Parameters and arguments are exactly the same thing (parameters is the official term, people like me call them arguments). And if you had put the definition of the function (the thing you have at the bottom) in place of the prototype, you would declare and define at the same time.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Think I am slow learner...I would like to see some examples if possible. And I hope it doesn't bother you.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    #include <iostream>
    
    using namespace std;
    
    //declaring and defining at the same time
    void print_out (int n)
    {
        int i;
        for(i = 1; i <= n; i++)
            cout<< i << endl;
    
    }
    
    //a prototype
    int mymax(int first, int second);  //note that int mymax(); would be an error
                                       //it would designate a different function with no arguments
                                       //so C++ would think you had two functions with the same name
                                       //you're allowed to do that if they have different parameter lists
    
    int main()
    {
        int n;
        cout<< "Enter a number: ";
        cin>> n;
        print_out(n);
    
    return 0;
    }
    
    //Here's the function promised earlier
    int mymax(int first, int second) {
        if (first > second) {
            return first;
        } else {
            return second;
        }
    }

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    (parameters is the official term, people like me call them arguments
    Not quite. kenryuakuma, skip this post, it doesn't contain anything of value to a newbie. (Or, for that matter, an experienced programmer. This is something for language lawyers.)

    For those who care, the C++ standard uses the terms "argument" and "parameter" very specifically. An argument is the value/expression you pass to a function; a parameter is the variable that receives it. Also referred to as "actual argument/parameter" and "formal argument/parameter", respectively.
    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

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    <Also to be ignored>
    Hey, I didn't realize that. I see that now in the C standard too. I'm assuming that the distinction is carried through in the text but I had never quite assimilated the difference. (And obviously I didn't read the "definitions" section.)

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Quote Originally Posted by tabstop View Post
    The above example, your post #4, is not correct. void print_out() and void print_out(int n) are not compatible (you could get away with it in C, but not C++).

    Parameters and arguments are exactly the same thing (parameters is the official term, people like me call them arguments). And if you had put the definition of the function (the thing you have at the bottom) in place of the prototype, you would declare and define at the same time.
    Now I kind of understand what you say. If we wanna declare and define the function at the same time. We must define it in the very beginning before the main()? If we put it after the main(), we have to declare it first, which means we cannot declare and define it if we define the function after the main()?

    Besides, would you do me a favor by giving another example of the function with no argument? I would like to see what it is like and how it is done with no argument.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  2. book exercise
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 11-13-2005, 11:28 AM
  3. Exercise from my book.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 12-05-2002, 08:54 AM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 04:44 PM