DEV Community

kururu
kururu

Posted on

Dart basics - part 2

Image description

in the first post we covered some basics like variable and control structure
in to days lesson we are going to focus on Functions

Functions in Dart

generally , there are two type of functions in dart like other languages:
- function return a value
- function not return any value( void )

function form:
[type] function_name( [variables] ) {
[return ]
}

for example , function accepts two params and show the sum of them:

`
void sumTwoValues(int x, int y){
var sum = x+y;
print(sum);

}`

note: the return type is optional as dart can inferred the type:)

an example of the function with return type , let's create a function the accept a text and return a length of that text

`int textLngthGenerator(Stirng txt){

return txt.length;
Enter fullscreen mode Exit fullscreen mode

}`

to invoke this function:


var text= 'i love Sudan';
var textLength = textLngthGenerator(text);
print(textLength);

You noticed that you can use single quotes to represent a string

we stop here , and in the next post will discuss some topics related to functions like:
- higher-order functions
- callback functions
- extension functions

thanks for reading :)

Top comments (0)