Hi guys,
I have this code - I am trying to find if a number is present in a given array using recursion. The thing is, I get the error "not all code paths return a value". I don't know how to fix this. Please advise.

using System;


namespace checkArrayForValue
{
    class Program
    {
        static void Main(string[] args)
        {
            int target = 4;
            int[] arr = { 1, 4, 2, 5, 6, 7 };
            Console.WriteLine(FindTarget(target,arr,0));
        }
        public static bool FindTarget(int target, int[] arr, int index)
        {

            if (index >= arr.Length-1)
            {
                return false;
            }

            if (arr[index] == target)
            {
                return true;
            }

            FindTarget(target, arr, index + 1);

        }
    }
}

Falling off the end of a function doesn't return anything meaningful. What you want is to return the result of the recursion (see line 13):

public static bool FindTarget(int target, int[] arr, int index)
{
    if (index >= arr.Length-1)
    {
        return false;
    }

    if (arr[index] == target)
    {
        return true;
    }

    return FindTarget(target, arr, index + 1);
}

Wow, it was that easy! Thank you! :)

 //look it this..
        public static bool ISExistsArrayValue(int[] array, int findValue)
        {
            if (array.Length > 0)
            {
                var result = array.Where(s => s == findValue).FirstOrDefault();
                if (result > 0)
                    return true;
            }
            return false;
        }
commented: Totally irrelevant to the topic -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.