Results 1 to 6 of 6

Thread: When does a function calls itself?

  1. #1
    Join Date
    Dec 2010
    Posts
    66

    When does a function calls itself?

    Hello friends, I want to be programmer in future , but there are lot of hindrances and I get stuck at some point after a while , so I have visited your forum to take your help, I have got my exams now and I have lot of doubts in my subjects, but I wanted to ask about the functions and its behavior , I am quite aware that a function has a return type , but are there any cases when a function invokes itself, it was being said to me by my teacher but I could not understand it properly , and I tried to find it with the same keywords but could not get what I actually desired, please help me , I will be very grateful. Thank you in advance. I do not use any software such as NetBeans . I want to know does it exists only in Java or other object oriented language as well.

  2. #2
    Join Date
    May 2009
    Posts
    543

    Re: When does a function calls itself?

    Yes this mostly happen in recursion , you could not find it because you were not including the word recursion with your keyword ., merely put it as recursion , this mostly happens when a function invokes itself. That is, in the way of the function definition there is a invoke to that very similar function. Initially it may look like a never terminating loop, or like a animal chasing its tail. It may not be able to catch it. So too it seems your function will never come to an end. This may be true is a few situations case, but in practice we can verify to see if a few condition is true and in that situation exit (return from) your function. The situation in which you terminate your recursion is known as a base case . In addition, just as in a loop, we must alter a few values and incrementally move on closer to our base case.

  3. #3
    Join Date
    Apr 2009
    Posts
    569

    Re: When does a function calls itself?

    Consider this method:
    Code:
    void myfun( int cntr)
    {
    if(cntr == 0)
         return;
    else
           {
           System.out.println("the counter is " + cntr);
           myMethod(--cntr);
           System.out.println(""+cntr);
           return;
           }
    }
    If the function is called with the value 4, have you any idea what will the result be ? The above shown recursion is basically a loop similar to a for loop or a while loop. When do we favor recursion to an iterative loop? We make use of recursion when we observe that our issue can be made simpler or simplify to a certain extent can be solved after further decrease.

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: When does a function calls itself?

    Yes I too think that above given example is helpful a lot . The above recursion is essentially a loop like a for loop or a while loop. When do we prefer recursion to an iterative loop? We use recursion when we can see that our problem can be reduced to a simpler problem that can be solved after further reduction.
    Each recursion should have the characteristics as given below :.
    • A uncomplicated base case which we have a explanation for and a return value.
    • A method of getting our problem nearer to the base case., that is . a method to remove out fraction of the problem to get a rather easier problem.
    • A recursive invoke which passes the simpler issue back into the function.

  5. #5
    Join Date
    May 2009
    Posts
    511

    Re: When does a function calls itself?

    The solution to thinking recursively is to observe the solution to the issue as a smaller edition of the similar problem. The key to solving recursive programming necessities is to visualize that your function does what it is intended to and its name suggests it does even sooner than you have in fact finish writing it. You must act as if the function does its job and then use it to solve the more difficult cases.

  6. #6
    Join Date
    May 2009
    Posts
    511

    Re: When does a function calls itself?

    You would have done the code of deriving a factorial using recursion but the question comes , why do we need to use recursion, the answer is this question is mainly because it is simpler to to code a recursive solution once you are able to recognize that solution. The recursive code is generally smaller, more brief, more well-designed, perhaps even easier to comprehend, though that relies on your way of thinking . But also, there are a few issues that are very tricky and complex to solve without recursion. Those issues that need to go into reverse such as searching a maze for a path to an exit or perform operations are based on a tree ) are solved in the most finest manner recursively. There are also a few sorting algorithms that make use of recursion.

Similar Threads

  1. Replies: 3
    Last Post: 28-12-2011, 10:39 PM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  4. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  5. Function keys don't function under windows XP
    By GunFighter in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-04-2009, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,263,286.65911 seconds with 16 queries