Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  • 1. No Parameter And No Return Type
  • 2. Parameter And No Return Type
  • 3. No Parameter And Return Type
  • 4. Parameter And Return Type
  • Conclusion
  1. Functions

Types of Functions in Dart

Functions in Dart are blocks of code that perform specific tasks. Dart functions can be categorized into four main types based on whether they take parameters and whether they return values:

  1. No Parameter And No Return Type

  2. Parameter And No Return Type

  3. No Parameter And Return Type

  4. Parameter And Return Type

Let's explore each type with examples:

1. No Parameter And No Return Type

In this type of function, the function does not take any parameters, and it does not return a value. These functions are used when you need to perform a task without needing to pass or return data.

void greet() {
  print('Hello, Dart!');
}

In the example above, the greet function does not take any parameters (()) and does not return a value (void). It simply prints a greeting message.

2. Parameter And No Return Type

In this type of function, the function takes one or more parameters but does not return a value. These functions are used to perform actions that depend on input data.

void sayHello(String name) {
  print('Hello, $name!');
}

In the example above, the sayHello function takes a String parameter name and prints a greeting using the provided name.

3. No Parameter And Return Type

In this type of function, the function does not take any parameters but returns a value. These functions are used when you need to compute and return a result.

int getRandomNumber() {
  return 42; // A fixed random number for simplicity
}

In the example above, the getRandomNumber function does not take any parameters but returns an int value (42 in this case). You can replace the fixed value with actual computations.

4. Parameter And Return Type

In this type of function, the function takes one or more parameters and returns a value. These functions are used when you need to perform calculations based on input data and provide a result.

int add(int a, int b) {
  return a + b;
}

In the example above, the add function takes two int parameters, a and b, and returns their sum as an int value.

Conclusion

Understanding these four types of functions in Dart allows you to write organized and efficient code. Depending on your program's requirements, you can choose the appropriate type of function to perform specific tasks, manipulate data, or return results as needed.

PreviousfunctionParametersNextFunctions in Dart

Last updated 1 year ago