Thread: Bloody recursive thing

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Question Bloody recursive thing

    Hi!
    How are ya!
    Anyway, I've just begun learning programning and I was trying the whole recursive program tutorial. I dunno what the problem is but every time the recursive function returns an integer to main it changes into 30866468. Here is what I got:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int recursion (int x, int y);
    int main ()
    {
        int num_user;
        int num_result = 0;
        cout<<"Input a number\n";
        cin>> num_user;
        num_result = recursion(num_user, 1);
        cout<< num_result;
        _flushall();
        cin.get();
    }
    
    int recursion (int x, int y)
    {
        y *= x;
        if (x == 1)
        {
              return y;
        }
        recursion ((x - 1), y);
    }
    Thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > recursion ((x - 1), y);
    Try
    return recursion ((x - 1), y);

    A good compiler would tell you that you could return without a value.
    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.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    gcc, which you probably use, for some unfathomable reason requires you to specify the -Wall command line parameter to do that.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By tonderai76 in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2004, 12:49 PM
  2. Recursive or Threads
    By vasanth in forum C++ Programming
    Replies: 5
    Last Post: 01-18-2003, 10:30 PM
  3. Recursive Array Sort
    By Nakeerb in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2002, 08:27 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  5. Recursive Function
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 03:41 PM