title: Recursive Linear Search
body: <p>The code shown below works fine. It prints the position of the element found inside the if clause and exits. Whenever the element is not found, the function runs to max and returns 0 to calling function to indicate no elements has been found. </p>
  
  <p>However, I was pondering about returning the position of the element found, to the calling function rather than printing it. Since returning the position would just return to earlier instance of the function and not to the calling function, I am struck. How to achieve this ?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int RLinearSearch(int A[],int n,int key)
  {
      if(n&lt;1)
          return 0;
      else
      {
          RLinearSearch(A,n-1,key);
          if(A[n-1]==key)
          {
              printf("found %d at %d",key,n);
              exit(0);
          }
      }
      return 0;
  }
  
  int main(void) 
  {
      int A[5]={23,41,22,15,32};   // Array Of 5 Elements 
      int pos,n=5;
  
      pos=RLinearSearch(A,n,23);
  
      if(pos==0)
          printf("Not found");
  
      return 0;
  }
  </code></pre>