Functions

Jun's Coding Journey·2023년 8월 9일
0

[Learn] Dart

목록 보기
5/6

Defining a Function


Void means that a function does not return anything.

A function can be returned if we specify a function to have a data type instead of the void keyword.

For one-line codes, we can use arrow functions.

String sayHello(String name) {
  return "Hello $name nice to meet you!";
}
String sayHello(String name) => return "Hello $name nice to meet you!";

 
We call our returned functions within the main function.

void main() {
  print(sayHello('John'));
}

 

Positional Parameters


Positional parameters are a way to pass arguments to a function based on their position in the function's parameter list.

When you define a function with positional parameters, you list the parameters in a specific order, and when you call the function, you provide values in the same order.

This is not recommened, because users will have a hard time figuring out the parameters.

String sayHello(String name, int age, String country) {
  return "Hello $name, you are $age, and you come from $country";
}

void main() {
  print(sayHello('John', 19, 'USA'));
}

 

Named Parameters


Named parameters allow you to pass arguments to a function using the names of the parameters, rather than relying on their positions in the parameter list.

Named parameters are especially useful when a function has multiple optional parameters or when you want to provide clarity and prevent confusion in function calls.

Make sure to add curly brackets on parameters.

String sayHello({String name, int age, String country}) {
  return "Hello $name, you are $age, and you come from $country";
}

void main() {
  print(sayHello(
  	name: 'John',
    age: 19,
    country: 'USA',
  ));
}

 
If the user sends a null value, there are 2 solutions.

1) Provide default values.

String sayHello({
  String name = 'anonymous',
  int age = 99,
  String country = 'Wakanda',
}) {
  return "Hello $name, you are $age, and you come from $country";
}

2) Make the named parameters 'required'.

String sayHello({
  required String name,
  required int age,
  required String country,
}) {
  return "Hello $name, you are $age, and you come from $country";
}

 

Optional Positional Parameters


Using square brackets([]) we can use default values for positional parameters.

String sayHello(
  String name,
  int age,
  [String? country = 'USA'],
) => 'Hello $name, you are $age years old from $country';

void main() {
  print(sayHello('John', 19));
}

 

QQ Operator


Using ?? and ??= operators, we can make Dart deal with null values with cleaner code.

String capitalizeName(String? name) =>
  name.toUpperCase() ?? 'ANONYMOUS'; // either left or the right

void main() {
  capitalizeName('John');
  capitalizeName(null);
}
String capitalizeName(String? name) =>
  name.toUpperCase() ?? 'ANONYMOUS'; // either left or the right

void main() {
  String? name;
  name ??= 'John';
  name = null;
  name ??= 'Dave';
  print(name);
}

 

Typedef


Typedef is a way of making aliases for data types.

In case of maps, it is better to typedef on maps in classes.

typedef ListOfInts = List<int>

ListOfInts reverseListOfNumbers(ListOfInts list) {
  var reversed = list.reversed;
  return reversed.toList(); // convert back to list
}

void main() {
  reverseListOfNumbers([1, 2, 3]); // [3, 2, 1]
}

profile
Greatness From Small Beginnings

0개의 댓글