5. Dart - Function Basics
Function Parameters:
function(int variableint) {
print(variableint)
}
// lets functions use variables given when called.
// can add more than one parameter, just separate them with commas.
// can add a type (String, int, etc) prior to function name to specify it more.
// optional paramenters are encased with square brackets.
Adding Parameters to Function Calls:
function(5);
Returning Variables from Function:
function(int variablenum) {
variable = variablenum + 2
return variable;
}
int test = 5;
x = function(test);
// returns the variable to where it was called.
Return Example 2:
String functionname() {
return "string"
}
// declares the return type in front of the function name
Void:
void functionname() {
stuff here
}
// Indicates that the function does not and can not return anything,
Single Line Functions: (when want to return a single value)
function(int test) => print(test);
void function(int test) => print(test);
function(int a, int b) => a * b; //implied return is applied here
Comments
Post a Comment