Browse Source

docs(named-optional-parameters): correct grammar

DEV-4541-Go-go-reloaded-synchronous-project-reveiw
davhojt 1 year ago committed by Dav Hojt
parent
commit
6a1e9ab3e1
  1. 24
      subjects/mobile-dev/named-optional-sum/README.md

24
subjects/mobile-dev/named-optional-sum/README.md

@ -1,37 +1,37 @@
# Named Optional Sum
## Named Optional Sum
### Instructions
Write a function called `namedOptionalSum()` that accepts named parameters `first`, `second`, `third` and returns the sum of them.
Write a function named `namedOptionalSum`, that accepts named parameters `first`, `second` and `third`. It returns the sum of its parameters.
- All the parameters are integers.
- Absent parameters should be considered as 0.
Absent parameters should be considered as `0`.
### Named parameters
In Dart you can declare functions that require explicit naming for arguments. Compare 2 functions below.
In Dart you can declare functions that require explicit naming for arguments. Compare the following:
```dart
// Example 1
void someFunction(bool bold, bool hidden) {...}
someFunction(true, false);
```
```dart
// Example 2
// Now you must specify which argument you are referring to
void someFunction({bool? bold, bool? hidden}) {...}
someFunction(bold: true, hidden: false);
```
This way you must specify the name of argument each time you call a function. You can also skip the parameter by simply not specifying its name and value. What do you think will be the default value for skipped parameters?
This way you must specify the name of argument each time you call a function. You can also skip the parameter by simply not specifying its name and value.
> What do you think the default value will be for parameters which are skipped?
### Null safety
You might be wondering what does "?" sign in bool? mean.
You might be wondering what the `?` sign means in `bool?`.
Types in your code are non-nullable by default, meaning that variables can’t contain `null` unless you say they can. With null safety, your runtime null-dereference errors turn into edit-time analysis errors.
As Dart's documentation suggests - "...types in your code are non-nullable by default, meaning that variables can’t contain null unless you say they can. With null safety, your runtime null-dereference errors turn into edit-time analysis errors."
What happens if the argument of the function is optional and it is omitted? It should be `null`, but the Dart's null safety does not allow it, and you will get an error from the compiler. In order to let Dart's compiler understand that certain variables should be able to accept `null`, you **must** initialize primitives with a `?`.
What happens if the argument of the function is optional and it is omitted? It should be null, but the Dart's null safety does not allow it, and you will get error from compiler. In order to let Dart's compiler understand that certain variable should be able to accept null, you **must** initialize primitives with question sign. More on null safety [here](https://dart.dev/null-safety).
See [null safety](https://dart.dev/null-safety).

Loading…
Cancel
Save