Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was asked to make a function int return bool and I can't find a solution, I though you can't return bool in a function int.
I just need to know if is posivel.

What I have tried:

I tried to search everywhere but I couldn't find a solution and I couldn't either make one.
Posted
Updated 28-Apr-22 10:08am
Comments
The Other John Ingram 28-Apr-22 14:45pm    
return true
return false
return (test == 0)
The Other John Ingram 28-Apr-22 14:50pm    
if (test == 0)
return 1; // true
else
return 0; // false

hope this helps
Antonio_Rodrigues 28-Apr-22 15:20pm    
int testSum(int[] numbers = null, int b = 2)
{
numbers = new int[] { 8, 3, 9, 6, 7, 4 };

for ( int i = 0; i < 5; i++)
{
if(numbers[i] % b == 0)
{
return true;
}
else
{
return false;
}

}
}

what I have is this I just can't make it work
Maciej Los 28-Apr-22 15:45pm    
This should be a part of your question - inside a "What i have tried" section.

BTW: do you want to return boolean value if every number in an array divided by b is equal to zero?
Maciej Los 28-Apr-22 15:52pm    
You mean this:
void Main()
{

	int[] numbers = new int[] { 8, 3, 9, 6, 7, 4 };
	testSum(numbers).Dump();
}

int testSum(int[] numbers = null, int b = 2)
{
	return Convert.ToInt32(numbers.All(x => x % b == 0));
}

Something like this:
C#
private bool Foo(int x)
   {
   return x == 666;
   }
 
Share this answer
 
Comments
Maciej Los 28-Apr-22 15:57pm    
If i understand the question well, a function should return int.
If i understand you well...
Try this:
C#
Console.WriteLine($"true = {Convert.ToInt32(true)}; false = {Convert.ToInt32(false)}");
//prints: true = 1; false = 0


So, if you want to return boolean value out of int function, use 0 or 1. ;)

C#
int testSum(int[] numbers = null, int b = 2)
{
	return numbers.All(x => x % b == 0) ? 1 : 0;
}


If i misunderstood your question, please explain it better.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900