Created
November 25, 2020 12:12
-
-
Save olaide-ekolere/520e5b2a6bb8c88c326ecd3ea06480f7 to your computer and use it in GitHub Desktop.
Functions - Introduction to Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
//Variables | |
const double pi = 3.14; | |
void main() { | |
showPerimeter();//calling method with no parameters | |
showPerimeterWithRadius(80);//calling method with parameter | |
double result = perimeter(); | |
print(result); | |
var result2 = perimeterWithRadius(100); | |
print("Perimeter is $result2"); | |
showPerimeterWithMultiple("Perimeter of circle with multiple parameters and radius of", 120); | |
showVolume(length: 20, breadth: 30, height: 80); | |
//showVolume();//fix issue with default parameters | |
//showVolume(length: 20, breadth: 30); | |
//showVolume(height: 80); | |
showCubeVolume(length: 60); | |
showArea(); | |
showArea(3, 5); | |
print(anyArea(40)); | |
print(anyArea(25, breadth: 32)); | |
print(anyArea(13, breadth: 7, height: 91)); | |
sendMessage("Olaide", "Damilola"); | |
sendMessage("Esther", "Imole", "Hello World!!!"); | |
} | |
//Methods | |
void showPerimeter() { | |
int radius = 70; | |
double result = 2 * pi * radius; | |
print("Perimeter of circle with radius of $radius is $result"); | |
} | |
//Method with parameter | |
void showPerimeterWithRadius(int radius) { | |
double result = 2 * pi * radius; | |
print( | |
"Perimeter of circle with parameter radius of $radius is ${result.toStringAsFixed(2)}"); | |
} | |
//Method with return type | |
double perimeter() { | |
return 2 * pi * 70; | |
} | |
//Method with required position parameter and return type | |
double perimeterWithRadius(int radius) { | |
return 2 * pi * 70; | |
} | |
//Methods with required position multiple parameters | |
void showPerimeterWithMultiple(String prefix, int radius) { | |
double result = 2 * pi * radius; | |
print("$prefix $radius is ${result.toStringAsFixed(2)}"); | |
} | |
//Methods with Named optional parameters | |
void showVolume({int length, int breadth, int height}) { | |
var result = length * breadth * height; | |
print("The volume is $result"); | |
} | |
void showCubeVolume({@required int length}){ | |
showVolume(breadth: length, height: length, length: length); | |
} | |
//Methods with Optional Positional parameters | |
void showArea([int length = 1, int breadth = 1]){ | |
print("the area is ${length * breadth}"); | |
} | |
//methods with combined parameters | |
int anyArea(int length, {int breadth = 1, int height = 1}){ | |
return length * breadth * height; | |
} | |
void sendMessage(String sender, String receiver, [String message = "Ping"]){ | |
print("Message from $sender to $receiver is: $message"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment