Skip to content

Commit

Permalink
Expressions in Function. SHORT HAND SYNTAX
Browse files Browse the repository at this point in the history
  • Loading branch information
smartherd committed Jun 27, 2018
1 parent 74c306d commit 3de79fd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 14_function_expressions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// OBJECTIVE: Expression in Function: SHORT HAND SYNTAX

void main() {

findPerimeter(4, 2);

int rectArea = getArea(10, 5);
print("The area is $rectArea");
}

void findPerimeter(int length, int breadth) => print("The perimeter is ${2 * (length + breadth)}");

int getArea(int length, int breadth) => length * breadth;


// "=>" is known as FAT ARROW
// "=> expression" is a SHORT HAND SYNTAX for { return expression; }
// Example "=> length * breadth" is SHORT HAND SYNTAX for { return length * breadth; }
28 changes: 28 additions & 0 deletions 15_optional_positional.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

// 1. Required Parameters
// 2. Optional Positional Parameters

void main() {

printCities("New York", "New Delhi", "Sydney");
print("");

printCountries("USA"); // You can skip the Optional Positional Parameters

}

// Required Parameters
void printCities(String name1, String name2, String name3) {

print("Name 1 is $name1");
print("Name 2 is $name2");
print("Name 3 is $name3");
}

// Optional Positional Parameters
void printCountries(String name1, [String name2, String name3]) {

print("Name 1 is $name1");
print("Name 2 is $name2");
print("Name 3 is $name3");
}
19 changes: 19 additions & 0 deletions 16_named_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// Optional Named Parameters

void main() {
findVolume(10, breadth: 5, height: 20);
print("");

findVolume(10, height: 20, breadth: 5); // Sequence doesn't matter in Named Parameter
}


int findVolume(int length, {int breadth, int height}) {

print("Length is $length");
print("Breadth is $breadth");
print("Height is $height");

print("Volume is ${length * breadth * height}");
}
23 changes: 23 additions & 0 deletions 17_default_parameters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

// Optional Default Parameters

void main() {

findVolume(10); // Default value comes into action
print("");

findVolume(10, breadth: 5, height: 30); // Overrides the old value with new one
print("");

findVolume(10, height: 30, breadth: 5); // Making use of Named Parameters with Default values
}


int findVolume(int length, {int breadth = 2, int height = 20}) {

print("Lenght is $length");
print("Breadth is $breadth");
print("Height is $height");

print("Volume is ${length * breadth * height}");
}

0 comments on commit 3de79fd

Please sign in to comment.