How are parameters passed in dart (Class 17)

How are parameters passed in dart

While learning functions, it is important to know the answer of how are parameters passed in Dart. But before we will discuss another optional entity i.e. Return.

 

Return

Return is a keyword used in a function. It means that the function will return specific types of data. A function can only return one thing and exit at the retune keyword. The lines after the ‘return’ keyword never execute. It’s an optional part of a function to specify a return type. A function can have any return type e.g. int, String, double, bool, List, Set, Map, HashMap, or Object. The return will depend on the data type of returned value. Let’s see some examples of return to understand it completely:

int addition (){
  int no1 = 30, no2 = 40;
  int sum = no1+no2;
  print (sum);
  return sum;
}

 

String address() {
  String country = “Pakistan”, city = “Rawalpindi”;
  String address = “Country name is $country and city name is $city”;
  print(address);
  return address;
}

 

double gpa() {
  double student1 = 3.4, student2 = 4.0;
  print(“student 1 gpa is $student1 & student 2 gpa is $student2”);
  return student1;
}

 

Conditions for return

As clear from the above examples, we need to define the return type before function name. It is like a signature. As there is int, String, and, double variable type in the above examples that’s why the return is also the same. If we use some other variable return type then conflict/ error will appear. It is important to know that we cannot use var but dynamic can be used. But Dart recommends using exact type in return. If we use void then it will return nothing.

 

Now let’s move to our main topic i.e. how are parameters passed in Dart but before that we understand parameters.

Parameters

Parameters are another optional part of a function like return. It is in signatures of a function. SignatureThe signature art which comes before body. A function can have one type or different types of unlimited parameters. We will separate each parameter with comma ,. We can even create parameters without any type then it will accept any kind of value but it is not recommended by Dart. Dart recommends to write writing every params. It can be of list, map, var or dynamic type too. Params are accessible only inside the function body. The main difference between params and variables is that we cannot initialize parameters like variables.

 

How parameters work?

After the creation of params, we give value or initialize while calling it. We can also name parameters as params/Arguments/function-input/variable. We specify params inside the function’s parenthesis with the same naming rules of variables. Now let’s see some examples containing the same and different types of variables:

abc (int a, int b, int c, int d){
}
cde (String a, String b, String c) {
}

fgh (int a, var b, String c, double d, bool e, f, dynamic g) {
}

 

How are parameters declared?

After creating params we have to declare or give values to parameters according to their type. For this purpose, we will do so while calling this function. For example in previous examples of parameters we will give them values while calling as given below:

 

abc(4, 5, 6, 7);
cde(“Pakistan”, “Afghanistan”, “Sri lanka”);
fgh(4, 3, “robina”, 3.4, true, “f”, 45);

Task: Try yourself in Dart Pad.

Functions are very important in Flutter. So we need to practice them in order to work efficiently in Flutter.

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.