mirror of https://github.com/01-edu/public.git
davhojt
1 year ago
committed by
Dav Hojt
1 changed files with 11 additions and 13 deletions
@ -1,32 +1,30 @@ |
|||||||
# Optional Sum |
## Optional Sum |
||||||
|
|
||||||
### Instructions |
### Instructions |
||||||
|
|
||||||
Write a function called `optionalSum()` that accepts two integer arguments, and an optional integer argument. Return the sum of all the arguments. |
Write a function named `optionalSum` that accepts two `int` arguments, and an optional `int` argument. Return the sum of all the arguments. |
||||||
|
|
||||||
### Optional parameters |
### Optional parameters |
||||||
|
|
||||||
In Dart you can also make function arguments optional, meaning that a function can work even if the optional argument is omitted. If the optional parameter is omitted, it is considered to be null. |
In Dart, you can make function arguments optional, meaning that a function can work even if the optional argument is omitted. If the optional parameter is omitted, it is considered to be `null`. |
||||||
|
|
||||||
### Usage |
A function with optional parameters: |
||||||
|
|
||||||
Example of function with optional parameters: |
|
||||||
|
|
||||||
```dart |
```dart |
||||||
void someFunction(int firstParameter, int secondParameter, [int? optionalParameter]) { |
void someFunction(int first, int second, [int? third]) { |
||||||
if (optionalParameter != null) { |
if (third != null) { |
||||||
print('${firstParameter}, ${secondParameter}, ${optionalParameter}'); |
print('${first}, ${second}, ${third}'); |
||||||
} else { |
} else { |
||||||
print('${firstParameter}, ${secondParameter}'); |
print('${first}, ${second}'); |
||||||
} |
} |
||||||
} |
} |
||||||
void main() { |
|
||||||
|
|
||||||
|
void main() { |
||||||
someFunction(1, 2); |
someFunction(1, 2); |
||||||
someFunction(1, 2, 3); |
someFunction(1, 2, 3); |
||||||
} |
} |
||||||
``` |
``` |
||||||
|
|
||||||
- Note: Optional parameters must come after the required parameters. |
> Optional parameters must come after the required parameters. |
||||||
|
|
||||||
- Note: You cannot use both optional and named parameters, you should choose only one of them. |
> You cannot use both optional and named parameters. |
||||||
|
Loading…
Reference in new issue