Thread: Noob problem - function call

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    Noob problem - function call

    Hi,

    I am trying out a recommended book, "C++ Without Fear," and am having a problem with one of the exercises in the book and on the cd.

    The exercise states "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 reutrn a vlaue. The function can be called a simple statement: print_out(n);"

    Having a bit of difficult doing this on my own, I copied and pasted the exercise from the cd that came with the book.

    Code:
    // Exercise 04.01.02
    // This program prints out numbers from 1 to n,
    //  by calling a function.
    
    #include <iostream>
    using namespace std;
    
    // Function must be declared before being used.
    
    void print_out(int n);
    
    int main() {
        int n;
    
        cout << "Enter a number and press ENTER: ";
        cin >> n;
    
        print_out(n);
    
        return 0;
    }
    
    // Print-out function.
    // Prints numbers from 1 to n. 
    
    int print_out(n) {
        int i;
    
        for (i = 1; i <= n; i++)     // For i = 1 to n,
            cout << i << " ";        //   print i
        return sum;
    }
    When I try to compile it, it highlights "int print_out(n)" and says, "int print_out redeclared as different kind of symbol."

    I don't really know what that means, and am confused as well since I am using the sample provided on the cd, so wonder why it isn't compiling. I am using Dev C++ 4.9.9.2.
    Last edited by Ultraman; 05-20-2006 at 02:31 AM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    Code:
    void print_out(int n) {
        int i;
    
        for (i = 1; i <= n; i++)     // For i = 1 to n,
            cout << i << " \n";        //   print i
    
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Take a close look:
    Code:
    int print_out(n) {
    This should be like the function prototype instead, i.e.
    Code:
    void print_out(int n) {
    But if print_out's return type is void, then:
    Code:
    return sum;
    Must be wrong. In fact, methinks you can safely remove it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void print_out(int n);
    Here you say it returns void

    > int print_out(n)
    Here you say it returns int

    Pick one as being right, and change the other to match it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    7
    Thanks to all of you. Now it works as it should (and better than that, I now get it).

    And yeah, that 'return sum' was left over from an earlier exercise. Quite out of place, eh? Whoever edited the stuff that went on the cd did a bit of a sloppy job, methinks, as I've found that a couple exercises are even missing completely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 10:01 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM