Home > Mobile >  I have a basic question on dart function:
I have a basic question on dart function:

Time:10-25

I have wrote a bit code in dartpad:

void main() {
  greet('Jack');
  double result = myFunction();
  print(result);
}

void greet (String personToGreet) {
  print('Hello, $personToGreet)
}

double myFunction () {
  double pi = 3.1415926;
  return pi * 2;
}

So, for myFunction part, why I can't do the same thing as I did for the greet function which changes the function type from double to void and replace the return keyword at the end and with a print statement to wrap up 'pi * 2'?

And for myFunction part in main function, can I just call this specific function directly instead of pass it into a variable and then print the variable out?

CodePudding user response:

Your function greet expects an argument of type String. That means you can't pass it result, which is a double. Instead try this:

void main() {
  greet('Jack');
  double result = myFunction();
  print(result.toString());
}

Also you seem to be missing a closing quote around Hello

  • Related