Reputation: 195
Id need i in the Iteration method to be the return value for Iteration()....at the moment it is in error saying it has no return value. I assume since its in the for statement.
using System;
class Program
{
int i = 1;
static string globalValue = "I was declared outside any method";
static void Main()
{
for (int b = 0; b < 101; b++)
{
Console.Write(Iteration());
}
}
static string FizzBuzz()
{
string f = "word1";
return f;
}
static string Buzz()
{
string b = "word2";
return b;
}
static int Iteration()
{
for (int i = 0; i < 101; i++)
{
return i;
}
}
}
Upvotes: 0
Views: 70
Reputation: 330
Without compiling, if you are saying that the Iteration function is complaining that all code paths do not return an integer, it is because the return is included in the for, which the compiler doesn't know if the loop will actually be ran or not. Perhaps:
static int Iteration()
{
int retValue = 0; // some default value
for (int i = 0; i < 101; i++)
{
retValue = i;
break; // break when you get the iteration you want
}
return retValue;
}
Although the code doesn't really make sense, it should work for you.
Upvotes: 0
Reputation: 185663
The C# compiler only has a limited capability of navigating your code in order to determine if your function will always return a value. While the code that you've written will always return, the compiler isn't "smart enough" to figure that out.
Just put a return -1;
at the end of the function after the loop to satisfy the compiler.
Though, of course, the code you have now doesn't make much sense, as Iteration()
will always return 0
. It's not going to go through the entire loop, since a function can only return one value. (Iterator blocks being a syntactic exception, but not an actual exception).
Upvotes: 4