Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Recursive method calling (is it wrong?)

Recursive method calling (is it wrong?)

Scheduled Pinned Locked Moved C#
comquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    therealmccoy
    wrote on last edited by
    #1

    I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com

    J D T 3 Replies Last reply
    0
    • T therealmccoy

      I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      It's not wrong, but could cause deadlocks if the condition is never true. Also, recursive functions are less efficient than a while loop because each function call in a recursive function must be added to a method call stack, whereas while loops do not. while(blnTriggerFileWritten == false) { blnTriggerFileWritten = ...; }

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Who is Jewish, the Trivia Game! Judah Himango

      1 Reply Last reply
      0
      • T therealmccoy

        I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #3

        It depends on what you're trying to do. The most readable version of a binary search is recursive. You should however be aware that there's overhead involved in the additional calls and that as a result the recursive version will take longer to execute than the nonrecursive one. If performance isn't an issue however the recrusive version will be easier to understand and maintain.

        1 Reply Last reply
        0
        • T therealmccoy

          I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com

          T Offline
          T Offline
          therealmccoy
          wrote on last edited by
          #4

          Thanks for the input. Was working a 'C' port and the other programmer was surprised that I could call my function from within my function. I will relook at the code and probably impliment your suggestions. Thanks again. www.lovethosetrains.com

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups