Table of contents
1.
Introduction
2.
Examples of main() Function
3.
Frequently Asked Questions
3.1.
Is it possible to write functions inside a function?
3.2.
Are we allowed to pass a function as an argument?
3.3.
Which type of variable takes a function as its value?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart’s main() Function

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be learning about the main() function of the dart. We have learned about the functions of the dart. If you haven't, go to this link. After we have written our code and when we come to the execution stage, the main() function is a point from where our execution starts. The main() function in our whole code is a top-level function and is also an essential and crucial part.

Inside the main() function, we write our whole logic, we call the other functions, declare our variables and write user-defined executables statements.

Return type: return type of main() function is void.

Parameter: it can have an optional parameter of type List<String>. It is used when we want to control our program from the outside, or we are coming from some other source.

Syntax: 

void main()
{
  //Code
}
//OR
void main(List<String> args)
{
  //Code
}

Examples of main() Function

Let us try out some examples to get a more rigid knowledge of the main() function.

Example: In this example shows a simple main() function.

Code:

void main()
{
  //printing the simple statement.
  print("Controller is in the main() function.");
}


Output:

Controller is in the main() function.

 

Example: In this example, we show how to call another function.

Code:

void otherFunction()
{
  print("Now, controller is in the otherFunction().");
}


void main()
{
  //printing the simple statement.
  print("Controller is in the main() function.");
  
  //calling other function from main() function.
  print("Calling the otherFunction(), but still in the main() function.");
  otherFunction();
}


Output:

Controller is in the main() function.
Calling the otherFunction(), but still in the main() function.
Now, controller is in the otherFunction().

 

Example: In this example, we show how to call multiple functions.

Code:

int fun2(int x, int y)
{  
  //function to calculate the subtraction
  //returing the subtraction
  return (y - x);
}


int fun1(int x, int y, int z)
{  
  //Storing the result of subtraction.
  int sub = fun2(x, y);
  //returning the result.
  return (sub * z);
}


void main()
{
  //now see an example of nested function calling.
  int x = 10, y = 20, z = 30;
  int res = fun1(x, y, z);
  //We will apply (y-x)*z on our variables and store result in res.
  print("The result after applying operation: $res");  
}


Output:

The result after applying operation: 300

Explanation: Here from the main() function we are calling a function fun1(), which afterwards calls a function fun2(). fun2() returns value to fun1() and fun1() returns value to main() function.

Frequently Asked Questions

Is it possible to write functions inside a function?

Yes, we can write the functions inside a function in the dart, but we need to call the function to use it.

 

Are we allowed to pass a function as an argument?

The anonymous function gives us the freedom to pass a function as an argument. After we pass a function as a parameter, it is unnamed that's why it is an anonymous function.  

 

Which type of variable takes a function as its value?

After we pass a function as an argument, the variable that takes that anonymous function is of type Function.

Conclusion

In this article, we have thoroughly discussed the main() function of the dart and its implementation. We have discussed how to pass parameters and also discussed some examples.

 We hope that this article has helped you enhance your knowledge regarding the main() function. Do check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo not forget to upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass