What is Function in Dart

In this article, I will describe about dart function.
  • 2159

Function in dart language

Function is a self-contained  program segment that carries out some well defined specific task.

Syntax of function

returnType functionName(arguments)
{
//
Body
//
}

Example of function

void main()

{

Say()

{

  return "I am a user defined function!";

}

 

Says() => 'I am also a user defined function';

print(Say());

print(Says());
}


output

fun.png

You can used (=>) syntax , instead of return statement.


The dart function contains different type of parameter-

  • Optional parameters

    In optional parameters, you can leave it's parameters and it's parameters returns null value.

Example of Optional parameters
 

void main()

{

Say(value, to, [from]) => '${from}  ${value} from ${to} ';

print(Say('comes','optionalParameter'));
}

Output

optionsl parameter content.png

  • Named Parameter

    Optional parameter are also named parameter.

Example of named parameter

void main()

{

Say(value, to, [from]) => '${from}  ${value} from ${to} ';

print(Say('comes','namedParameter','from:sharad'));
}

Output

 named para.png

 
Got a programming related question? You may want to post your question here
 
© 2019 DotNetHeaven. All rights reserved.