Tags

, , ,

How Functions or Methods Work in Dart? Here is a quick tip that can teach you the basic programming knowledge of Dart.

Learn Dart the Hard Way

We need to understand a few important features of functions before we dig deep into object-oriented programming again. The proper understanding of functions will help us to understand methods inside a class.
First of all, there are functions that just do nothing.
It called: ‘void’.
Let us consider this code:

//code 3.15
main(List arguments){
print(showConnection());
}
//optional positional parameter
String myConnection(String dbName, String hostname, String username, [String optionalPassword]){
if(optionalPassword == null){
return "${dbName}, ${hostname}, $username";
} else return "${dbName}, ${hostname}, $username, $optionalPassword";
}
void showConnection(){
myConnection("MySQL", "localhost", "root", "*******");
}

We have declared the function ‘showConnection()’ as ‘void’ and want to return it through the ‘main()’ function. We have this output:

//output of code 3.15
bin/main.dart:4:9: Error: This expression has type ‘void’ and can’t be used.
print(showConnection());

We cannot use the type ‘void’. If we use, we cannot return something through that function. So from a function we always expect something. We want a function to return a value.
So we are going to change the above code and write it this way:

//code 3.16
main(List arguments){
var myConnect = myConnection(“MySQL”, “localhost”, “root”, “*******”);
print(myConnect);
}
//optional positional parameter
String myConnection(String dbName, String hostname, String username, [String optionalPassword]){
if(optionalPassword == null){
return “${dbName}, ${hostname}, $username”;
} else return “${dbName}, ${hostname}, $username, $optionalPassword”;
}

A simple program to express the database connections parameters. In the above code, we have used a new concept called the optional parameter. You have already known that if we declare parameters or arguments in our functions, we have to pass them as it is. Otherwise, it gives us errors. However, we can use the concept of ‘optional parameters’. In the function ‘myConnections()’ we have passed four arguments.

Watch this line:

String myConnection(String dbName, String hostname, String username, [String optionalPassword]){}

We have written the last argument as [String optionalPassword]. It means this argument is optional. You do not have to pass it when you call the function. Here the logic is simple. If the optional parameter ‘optionalPassword’ is not defined when we pass it inside the ‘main()’ function, it is treated as ‘null’. Since it has been defined and passed it afterward, it is not null. Therefore, we have got this output:

//output of code 3.16
MySQL, localhost, root, *******

Now, we change the above code slightly and will not pass that argument anymore.
//code 3.17
main(List arguments){
var myConnect = myConnection("MySQL", "localhost", "root");
print(myConnect);
}
//optional positional parameter
String myConnection(String dbName, String hostname, String username, [String optionalPassword]){
if(optionalPassword == null){
return "${dbName}, ${hostname}, $username";
} else return "${dbName}, ${hostname}, $username, $optionalPassword";
}
The output also changes:
//output of code 3.17
MySQL, localhost, root

Compare these two lines before and after and we will only then understand why optional parameter is important:

//code 3.16
var myConnect = myConnection("MySQL", "localhost", "root", "*******");
//code 3.17
var myConnect = myConnection("MySQL", "localhost", "root");

If we did not declare it as optional, it would have given us an error. Let us change the optional parameter and see what type of error we get.

//code 3.18
main(List arguments){
var myConnect = myConnection("MySQL", "localhost", "root");
print(myConnect);
}
//optional positional parameter is no more
String myConnection(String dbName, String hostname, String username, String optionalPassword){
if(optionalPassword == null){
return "${dbName}, ${hostname}, $username";
} else return "${dbName}, ${hostname}, $username, $optionalPassword";
}
Now optional parameter is no more, and for that reason, we encounter this error in the output:
//output of code 3.18
bin/main.dart:3:31: Error: Too few positional arguments: 4 required, 3 given.
var myConnect = myConnection("MySQL", "localhost", "root");
^
bin/main.dart:8:8: Context: Found this candidate, but the arguments don't match.
String myConnection(String dbName, String hostname, String username, String optionalPassword){

Since there was no optional parameter we had to pass the fourth argument. In the previous code snippets, we did not have to do that.
Since in Dart, a function is an object, the same concept is true in case of methods and you will find it when we will discuss object-oriented programming again.

Advertisements