Variables and Functions

Variables are the namespace in memory that stores values. The name of a variable is called as identifiers. They are the data containers, which can store the value of any type. For example:

var myAge = 50;  

Here, myAge is a variable that stores an integer value 50. We can also give it int and double. However, Dart has a feature Type Inference, which infers the types of values. So, if you create a variable with a var keyword, Dart can infer that variable as of type integer.

Besides variable, Functions are another core feature of any programming language. Functions are a set of statements that performs a specific task. They are organized into the logical blocks of code that are readable, maintainable, and reusable. The function declaration contains the function name, return type, and parameters. The following example explains the function used in Dart programming.

//Function declaration  
num addNumbers(num a, num b) {  
    // Here, we use num as a type because it should work with int and double both.  
    return a + b;  
}  
var price1 = 29.99;  
var price2 = 20.81;  
var total = addNumbers(price1, price2);  
var num1 = 10;  
var num2 = 45;  
var total2 = addNumbers(num1, num2);

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *