Warning: Undefined array key "lang_code" in /home/pmfnldmy/public_html/androidatc/ap/extension/classes/common.php on line 800
Functions - Dart Fundamentals

Functions - Dart Fundamentals

A function in Dart or in any programming language has a specific name and has a set of programming statements. The function can be called at any location of the program to run the statements it has and returns a result, or performs some operations. This process saves time and effort in writing all the function statements one time, then at a certain location of the code call this function by its name to perform a specific procedure or return a value. Also, functions help in organizing the program to structured parts which help in program maintenance and any future modifications.

Each function has a unique name which is used to call it inside the Dart program several times without the need to duplicate statements in multiple source code files.

Most programming languages come with a prewritten set of functions that are kept in a library.

Function Structure

The following image displays the main components of the main function:

dart programming functions

Creating a Function

If you have a number of code statements that need to be used more than once within your program, you can gather them inside a function which has a specific name and then call this function within the program as many times as needed.

Example: In the following example, if you run the following code, the compiler will run the code which is in the main function only.

main() {
}

PrintSomething(){

  print("Hello Functions !!");

  print("Welcome to Canada");
}


Since we don’t have any code inside the main() function, the run output result will be empty as illustrated in the following figure:

dart programming function

If you call the PrintSomething() function inside the main()as illustrated in the following figure:

Flutter fundamentals

The code will be as follows:

main() {
PrintSomething();
}
PrintSomething(){
  print("Hello Functions !!");
  print("Welcome to Canada");
}


Now the run result will be as follows:

Dart training

Function Return Data Types

One purpose of using functions is to return value to the main function. The following example shows how to return value to the main function : main()

main() {
CourseName();
}

String CourseName(){
  return "Flutter Application Development";
}


The CourseName() function returns this string sentence : "Flutter Application Development"
If you run the above code, nothing will happen . The run result will be as follows:

Dart code - functions

As you see in the next code, we must add a print() function to display the CourseName()function return:

main() {
print(CourseName());

}

String CourseName(){
  return "Flutter Application Development";
}


The run output for this code follows:

flutter programming

Example: In the following example, the SumCalculator function will return an integer value of the sum of two numbers to the main() function.

main() {
  var result=SumCalculator(3, 7);
print("Sum Result = $result");
}

int SumCalculator(x,y){
  var z=x+y;
  return z;
}


The run output of this code follows:

Flutter Fundamentals

The same previous code can be written in the following way and you will get the same run output:

main() {
  print("Sum Result = ${SumCalculator(3, 7)}");
}

int SumCalculator(x,y){
  var z=x+y;
  return z;
}


Note: In the previous example, it is important to add the return command at the end of the SumCalculator function. This will return the result of the addition operation, variable z, to the location from where the function was called.

Example:
In the following code, the age() function will return a Boolean value (true or false) depending on the value of age “x” :

main() {
print(age(20));
}

bool age(x){
  if(x>=18){
    return true;
  }
  else{
    return false;}
}


The run output of this code follows:

dart function

Void Function

A void function returns values by modifying one or more parameters rather than using a return statement. Implicitly, if you don’t add anything before the name of the function, then it is a void
function.

Example:
In the following code, the function MyFunction()will not return any value. This function will work when you call it inside the main()function.

main() {
  MyFunction();
}

 void MyFunction(){
  var FirstName='George';
  if(FirstName.startsWith('G')){
    print("The first name starts with G");
  }
    else {
    print("The first name does not start with G");
  }
}


The run result of this code follows:

dart function

All functions by default are void functions ; therefore, in the previous example, if you remove the "void" before the function name, you will get the same result.

Function Returning Expression

You can use “=>” operator for returning a value in a brief way.

Example: The following code includes a return function:

main() {
  print('The Sum Result is: ${sum(3,5)}');
  print('The Multiply Result is : ${multiply(3,5)}');
}

sum(x,y){
  var z=x+y;
  return z;
}

multiply(x,y){
  var w=x*y;
  return w;
}


The run result follows:

dart function

However, if you use the => operator to return the expression "x+y" or "x*y" , you will get the same result but with less number of code lines, as illustrated in the following example:

main() {
  var x=3;
  var y=5;
  int sum()=>x+y;
  int multiply()=>x*y;
  print('The Sum Result is : ${sum()}');
  print('The Multiply Result is : ${multiply()}');
}


The run result follows:

dart function

Functions and Variable Scope

The location of variables - whether inside or outside the main() or other functions - has an important effect on using these variables and the function return value. The following examples display how the program workflow will be affected if we change the location of variables inside or outside the functions.

Example:
The following Dart code includes a function called CompanyName()which is used to print the company name value. In this example, the variable name is declared outside any function; therefore, it is called a global variable. In this case, it can be used in any function in this Dart program.

var name='Microsoft';
main() {
CompanyName(name);
}
CompanyName(name){
  print('My Company Name is : $name');
}


The run result of this Dart program follows:

dart function

Now, as illustrated in the next code, we will configure the name variable inside the CompanyName(name) function . This means that name variable is considered as a local variable for CompanyName(name) function.

var name='Microsoft';
main() {
  CompanyName(name);
}
CompanyName(name){
  var name='Google';
  print('My Company Name is : $name');
}


Since the local variable will be dominant over any other configurations that may come from outside this function, the run result of this code will be follows:

Also, the same thing applies in the following code. The local variable will take effect over the global variable:

var name='Microsoft';

main() {
  CompanyName('Android ATC');
}
CompanyName(name){
  print('My Company Name is : $name');
}


The run result of this code follows:

dart function

* This topic is a part of lesson 3 of Flutter Application Development course. For more information about this course, click here.