FAQ: Method Output - Return Errors

This community-built FAQ covers the “Return Errors” exercise from the lesson “Method Output”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Return Errors

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

How come
bool answer = true;
return answer;
works but
return bool answer = true;
doesn’t?

5 Likes

I was wondering the same.

Also, heads up for anyone running into the same issue: sometimes an error persists even after correcting it - meaning the console still throws an error message. You need to reload the whole page in that case.

1 Like

■■■■ guys I’m still stuck with that bool I can’t seem to figure out why I still have an error

1 Like

Hi did u figure it out ? :smiley:

using System;

namespace ReturnErrors
{
class Program
{
static void Main(string args)
{
Console.WriteLine(DecoratePlanet(“Mars”));
Console.WriteLine(“Is Pluto really a dwarf…?”);
Console.WriteLine(IsPlutoADwarf(true));
Console.WriteLine(“Then how many planets are there in the galaxy…?”);
Console.WriteLine(CountThePlanets());
}

static string DecoratePlanet(string planet)
{
   return $"*..*..* Welcome to {planet} *..*..*";
}

static bool IsPlutoADwarf(bool answer)
{
        return true;
}

static int CountThePlanets()
{
  return Convert.ToInt32(Console.ReadLine());
}
}

}

Please fix this error CC. I love this website for making my life simpler in learning new code! Error Below (starts after step 3 and will not properly compile step 4 without this error persisting):

" Program.cs(23,14): error CS1525: Invalid expression term ‘bool’ [/home/ccuser/workspace/csharp-return-errors/c-sharp-hello-world-run-some-c-sharp.csproj]
Program.cs(23,19): error CS1002: ; expected [/home/ccuser/workspace/csharp-return-errors/c-sharp-hello-world-run-some-c-sharp.csproj]

The build failed. Fix the build errors and run again. "

My code:

" using System;

namespace ReturnErrors
{
class Program
{
static void Main(string args)
{
Console.WriteLine(DecoratePlanet(“Mars”));
Console.WriteLine(“Is Pluto really a dwarf…?”);
Console.WriteLine(IsPlutoADwarf());
Console.WriteLine(“Then how many planets are there in the galaxy…?”);
Console.WriteLine(CountThePlanets());
}

static string DecoratePlanet(string planet)
{
   return $"*..*..* Welcome to {planet} *..*..*";
}

static bool IsPlutoADwarf()
{
  return bool answer = true;
}

static string CountThePlanets()
{
  return "8 planets, usually";
}
}

}
"

{
class Program
{
static void Main(string args)
{
Console.WriteLine(DecoratePlanet(“Mars”));
Console.WriteLine(“Is Pluto really a dwarf…?”);
Console.WriteLine(IsPlutoADwarf());
Console.WriteLine(“Then how many planets are there in the galaxy…?”);
Console.WriteLine(CountThePlanets());
}

static string DecoratePlanet(string planet)
{
   return $"*..*..* Welcome to {planet} *..*..*";
}

static bool IsPlutoADwarf()
{
  return true;
}

static int CountThePlanets()
{
  
  return 8;
}
}

}

static string DecoratePlanet(string planet)
{
return $"…* Welcome to {planet} …*";
}

static bool IsPlutoADwarf()
{
return true;
}

static int CountThePlanets()
{

return 8;

bool answer=true

defines a bolean variable and sets it to true, the same is

bool answer; 	 // variable definition 
answer = true;	 // set a value to the variable

Good practice recommends that variables definitions to be the first on our methods or programs
We shouldn’t create variables at the middle or at the end of the programs. We can’t use them before declaring it.

Then you can return a boolean value with

return answer;

or

return true;

Watch the steps:

1. Define a variable;
2. Set it a value;
3. Return it's value;

With

return bool answer=true;

you are defining a variable, setting it a value and returning it’s value in just one line; that’s doesn’t exist in C# language (or any other programing language…)

6 Likes

return answer on another line, after declaring its value

It seems like you can use strings or values without declaring the variable first. Why Is that?

namespace ReturnErrors

{

class Program

{

static void Main(string[] args)

{

  Console.WriteLine(DecoratePlanet("Mars")); 

  Console.WriteLine("Is Pluto really a dwarf...?");

  Console.WriteLine(IsPlutoADwarf());

  Console.WriteLine("Then how many planets are there in the galaxy...?");

  Console.WriteLine(CountThePlanets());

}



static string DecoratePlanet(string planet)

{

   return $"*..*..* Welcome to {planet} *..*..*";

}



static bool IsPlutoADwarf()

{

  bool answer = true;

  return answer;

}



static string CountThePlanets()

{

  return "8 planets, usually";

}

}

}

I figured for DecoratePlanet to contain some sort of Console.readLine with a defined variable and string data type as it needs input from another method to finish out the string it’s own method used. This goes for CountThePlanets as well. It returns a value without ever declaring a variable or data type within the method. Is this pretty much declared within the input and output parameters and the system just recognizes it?

1 Like

By the way there are about 100 billion planets in our galaxy. there are only 8 planets usually in our solar system.