I'm using Visual Studio, while I was running the code, I got this warning: "warning C4715: 'sumOfElement' : not all control paths return a value. " And the program always stop executing when it asks me to enter the integer value for x.

A for loop is an example of iteration, not recursion. There's a strict equivalence between the two, but they aren't the same thing - they can achieve the same end results, but do it in different ways. Recursion doesn't have explicit loops - it has functions that call themselves (perhaps indirectly, via other functions).
–
Steve314May 28 '13 at 23:58

2 Answers
2

Your approach here isn't really recursive. The idea with recursion is to consider a base case, and then consider how to reduce the problem at each step until you get to the base case.

For this problem:

The base case is when the length of the array is zero. In this case we return a sum of zero. (Intuitively: if the array is empty then we're adding nothing, giving a sum of zero.)

In order to reduce our array we look at the last element of the array (ie. at lengthOfArray - 1). We process this element: if it's less than x we add it, if it's not then we ignore it. We then get the result of processing the rest of the array by the same means (by calling the same function, but with a different array length), and add our result if applicable.