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. Functions

functionParameters

In Dart, function parameters are used to pass values into functions.
Parameters allow you to make your functions more flexible and reusable
by accepting input data that can be used within the function's body.
Dart functions can have zero or more parameters, and each parameter has a data type and a name.

Here's how you define function parameters in Dart:

returnType functionName(parameter1Type parameter1Name, parameter2Type parameter2Name, ...) {
  // Function body
  // Code that uses the parameters
}

Let's break down the key components of function parameters in Dart:

  1. returnType: This specifies the data type of the value that the function will return. If the function doesn't return a value, you can use void.

  2. functionName: This is the name of the function.

  3. parameterType: This is the data type of the parameter. It specifies what kind of values the parameter can accept.

  4. parameterName: This is the name of the parameter. It is used within the function to refer to the value passed as an argument when the function is called.

Here are some examples of functions with different types of parameters in Dart:

Function with No Parameters:

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

In this example, the sayHello function does not have any parameters.

Function with One Parameter:

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

In this example, the greet function has one parameter of type String named name. It accepts a name as input and uses it within the function.

Function with Multiple Parameters:

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

In this example, the add function has two parameters, both of type int. It accepts two integers (a and b) as input, performs addition, and returns the result.

Function with Optional Parameters:

You can make parameters optional by enclosing them in square brackets []. Optional parameters allow you to call a function without providing values for all parameters.

void printMessage(String message, [int? times]) {
  for (var i = 0; i < times ?? 1; i++) {
    print(message);
  }
}

In this example, the printMessage function has one required parameter (message) and one optional parameter (times). The times parameter is enclosed in square brackets, making it optional. If times is not provided when calling the function, it defaults to 1.

Function with Named Parameters:

You can use named parameters by enclosing them in curly braces {}. Named parameters allow you to pass arguments to a function in any order, making function calls more expressive.

void greet({String? firstName, String? lastName}) {
  print('Hello, ${firstName ?? 'User'} ${lastName ?? ''}!');
}

In this example, the greet function has two named parameters, firstName and lastName. You can call the function with named arguments like this: greet(firstName: 'John', lastName: 'Doe').

Understanding how to define and use function parameters in Dart is essential for creating flexible and versatile functions that can handle a variety of inputs.

PreviousFunctionsNextTypes of Functions in Dart

Last updated 1 year ago