Learning Dart part 2: functions
here the extended version of dart programing. we will discuss about functions. let dig into it.
3 functions
3.1 simple return type or non return type
Code 1:
Void main(){
printName();
}
//function none return
Void printName(){
print(“Janitha T”);
}
Code 2:
// function return type
Void main(){
String name=printName();
print(name)
}
//function none return
String printName(){
Return “Janitha T”;
}
3.2 simple return type with multiple values as return
Code 1:
// function return type
Void main(){
Var personalInfo=printPersonalData();
print(personalInfo.$1)// 24 nothing but first out
print(personalInfo.$2)// Janitha T nothing but second out
}
//function none return
(Int,String) printPersonalData(){
Return (”24” ,“Janitha T”);
}
Code 2:
// function return type
Void main(){
Var (age,name)=printPersonalData();//map the output from multi out function
print(age)// 24 nothing but first out
print(name)// Janitha T nothing but second out
}
//function none return
(Int,String) printPersonalData(){
Return (”24” ,“Janitha T”);
}
Code 3:
Void main(){
print(funcstr());
}
String? funcstr(){
Return null;// null output as a case then need to return type in the function as to add “?” into it
}
After the return statement, references go out from the function body. after return is there any statement which is not executing because the return keyword tells the computer for execution the function body is over.
3.3 input parameters to the function
Get janitha thalagoda’s stories in your inbox
Join Medium for free to get updates from this writer.
Code 1:
Void main(){
String name=”Janitha T”;
function(name);
}
Void function(input){
print(input);
}
Arguments as input for a function
Code 2:
void main(){
String name=’Janitha T’;
int age=38;
String greetings= ‘hello, sir, how can i help you?’;
helpMe(name:name,age:age,greetings:greetings);
}
void helpMe({required String name, required int age, required String greetings}){
print(‘${greetings} , my name is ${name}’);
print(‘I am ${age} years old’);
}
When there is no essential value needed to pass like age in code 3, this is how it can be written.
Code 3:
void main(){
String name=’Janitha T’;
int age=38;
String greetings= ‘hello, sir, how can i help you?’;
helpMe(name:name,greetings:greetings);
}
void helpMe({required String name, int? age, required String greetings}){
print(‘${greetings} , my name is ${name}’);
print(‘I am ${age} years old’);
}
Argument type output from the function
Code 4:
void main(){
final output=helpMe();
print(‘${output.greetings} , my name is ${output.name}’);
print(‘I am ${output.age} years old’);
}
({String name,int age,String greetings}) helpMe(){
String name=’Janitha T’;
int age=38;
String greetings= ‘hello, sir, how can i help you?’;
return (name:name,age:age,greetings:greetings);
}
Code 5:
void main(){
String name=’Janitha T’;
int age=38;
String greetings= ‘hello, sir, how can i help you?’;
helpMe(name:name,age:age,greetings:greetings);
}
void helpMe({required String name, required int age, required String greetings})=>
print(‘${greetings} , my name is ${name} \n I am ${age} years old’);








