What are the three types of parameters (Class 18)

What are the three types of parameters

The concept we are going to learn i.e. what are the three types of parameters is very important in the context of Flutter. So if we understand it we can easily work on flutter.  

 

What are the three types of parameters?

There are three types of parameters:

  1. Required Parameters
  2. Optional Named Parameters
  3. Optional Positional Parameters

Let’s discuss them in detail

 

Required Parameters

As clear from its name, these are mandatory parameters and we have to give value to the required parameters. We write them in parenthesis ( ) and need to call them in the main function. We cannot miss anyone of them. Let’s see the required parameters example:

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

 

How to call required parameters

We need to give values to every variable according to its type while calling it. As we will give values in the above example of the abc function as under:-

main() {
  abc(5, “anything”, “Pakistan”, 4.3, true, “anything”, 4);
}

 

Optional Named Parameters

These parameters are more flexible than the required parameters. We have the authority to call them or not. We write it in curly brackets { }. In our work we mostly use it. It is easy to read as we need to write the parameter name and then the value while calling it. We also have to initialize while making it or make it nullable. But Dart recommends to initialize it rather than making it nullable. We can write it in any sequence suitable for us. Let’s check its example:-

calculator (int a, int b, String c, String d, {String firstName=”table”, String lastName=”chair”, double gpa = 3.4, double cgpa = 3.5, double percentage = 60.1}) {
}

 

How to call Optional Named Parameters

We need to write its variable name to call Optional Named Parameters, use a colon : and then enter its value. We can also use the shortcut of Ctrl+P to see all the parameters we have created. Another ease is, by writing some alphabets, and while pressing enter, it will automatically be written. These little tricks can ease our work. Now we will see how to call the above calculator function. As elaborated before, we have to call required parameters whereas we have to liberty to call optional named parameters at our will as under:-

main() {
  calculator(10, 20, “Pakistan”, “we love”, lastName: “Qureshi”, gpa: 4.3);
}

 

Optional Positional Parameters

As clear from its name, the sequence or position of these parameters is very important and cannot be altered. We write them in square brackets [ ]. Moreover, we have to initialize parameters like the required parameters. Let’s check its example:-

addNumbers2([int a = 0, int b = 0, int c = 0]) {
}

 

How to call optional positional parameters

We don’t need to mention the variable name while calling it. We just give value but in the same sequence or position. Now let’s give value to the above addNumbers function:

main() {
  addNumbers2(2, 3, 4);
}

 

Important Conditions to be Remembered

There are some important conditions related to the above three types which need to be remembered. Let’s check them:-

  • We can have both required and optional parameters in a function but first, we have to write required and then optional parameters as per the following example: void addNumbers (int a, int b {int c, int d})
  • A function can have optional named parameters only once. It cannot be repeated. This means there will be only one curly bracket in the parameters.
  • Optional parameters would always be at the end of parameters after the required parameters. There would be no other parameters after the optional parameters.
  • We should use commas, after every parameter even before optional named parameters.
  • We can only write optional named or optional positional parameters at a time. Both cannot be combined.
  • We can also print or use these parameters in functions.
  • Required parameters should be in the same sequence. But we can have our choice/ sequence on optional named parameters.
  • We can make any optional named parameter as a required parameter by writing required with it. What’s its use? Can we not simply convert it to the required parameter instead of using the required in the optional named parameter? Its answer is we can do that but it is done for better readability as while calling it we have to write the variable name and then value. It is the reason why we mostly use this technique in our work and there would only be 1-2 required parameters during our practical work. Let’s see its example: calc(int a, int b, {required int c = 0, int d = 0}) {
      print(a + b + c + d);
    }

    main() {
      calc(30, 40, c: 30, d: 40);
    }

 

Function-loading

Calling the same function with a different number of parameters with different sequences of parameters and with different types of parameters is called function loading. Let’s see its examples:

Function
addition(int no1, double no2, {int no3=0, int no4=0}) {}

 

Function loading

addition(10, 20.342, no3:30, no4: 40 );
addition(10,20.24, no4: 40, no3: 30 );
addition(10,20.234, no3:30, );
addition(10, 20.234, no3: 30, no4: 40);
}
Task: Try yourself function loading in Dart Pad.

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.