Skip to content

Instantly share code, notes, and snippets.

@vraj6198
Last active March 24, 2020 06:39
Show Gist options
  • Save vraj6198/438245784a9bd3fb40768f58481ebb34 to your computer and use it in GitHub Desktop.
Save vraj6198/438245784a9bd3fb40768f58481ebb34 to your computer and use it in GitHub Desktop.
Function in dart
//Define a function
/* void main(){
findperimeter();
}
void findperimeter(){
int length=4;
int breadth=5;
int perimeter= 2*(length+breadth);
print("the perimeter is $perimeter");
} */
//Pass parameter to a function
void main(){
findperimeter(4,8);
int reacArea = findArea(3,4);
print("the area of triangle is: $reacArea");
}
int findperimeter(int length, int breadth)
{
int perimeter = 2*(length+ breadth);
print("the perimeter = $perimeter");
}
// return value from a function
int findArea(int length, int breadth)
{
int area = length*breadth;
// print("the area of triangle is: $area");
return area;
}
// Function Definition : Collection of stmts rouped together to perform a operation
/* return type is optional as we can remove it but that if we use void as return type in dart but void in dart means we are not returning
anything from uor method body but void is also optional.
example: int findarea(int length, int breadth){
function body
return length*breadth; }
void findarea(int length, int breadth)
{
function body
return length*breadth;
}
// properties of function in dart
-> Function in Dart are objects.
-> Function can be assigned to be variable or passed as parameter to other function.
-> All function in dart return value
-> If no return value is specified the function return NULL
-> Specifying return type is optional but is recommended as per code convention */
/* named parameter
-> prevent error if they are large number of parameters
now take one exampl:
// here, the breadth and height are named parameters
// curly braces is used to define a named parameters i.e {}
int volume(int length, {int breadth, int height}){
return length * breadth * height;
}
var result = volume (2, breadth: 3, height: 4);
print(result);
var result1 = volume (3, height: 3, breadth: 4); // sequence doesnot matter
print(result1); */
void main(){
volume(1,b: 12, h:100);
print("");
volume (1, h:100, b: 12);
}
void volume(int l, {int b, int h}){
print("length is = $l");
print("breadth is = $b");
print("height is = $h");
print("the volume is: ${l*b*h}");
}
/* optional defualt parameters
-> you can assign default value to parameters
int volume(int length, int bradth,{int height= 10}){
return length * breadth * height;
}
var result = volume(2,3);
print(result); // 2*3*10=60
var result = volume(2,3,height: 20); // override the default value
print(result); // 2*3*20 */
void main(){
volume(1); // override the default value
// volume(1,12,h:100)
print("");
volume(1,b: 20, h:30);
print("");
volume(10,h:30,b:20); // posistional parameter
}
void volume(int l, {int b=15, int h = 10}){
print("length is = $l");
print("breadth is = $b");
print("height is = $h");
print("the volume is: ${l*b*h}");
}
void main(){
printcities("vadodara","delhi","mumbai ");
print("");
printcountry("india","usa");
}
// reqiured parameters
void printcities(String name1, String name2, String name3){
print("name1 is $name1");
print("name2 is $name2");
print("name3 is $name3");
}
// optional positional parameter
// if you want to print only optinal item than use sqr bracket in that particular type
void printcountry(String c1,String c2, [String c3] ){
print("name1 is $c1");
print("name2 is $c2");
// print("name3 is $c3");
}
// using short hand syntax to experssion in function
// expression in the function: SHORT HAND SYNTAX
// => known as fat arrow. if you are using fat arrow then you don't have to use {}
void main(){
findperimeter(4,8);
int reacArea = findArea(3,4);
print("the area of triangle is: $reacArea");
}
void findperimeter(int length, int breadth) => print("the perimeter = ${2*(length+ breadth)}");
// return value from a function
int findArea(int length, int breadth) => length*breadth;
// print("the area of triangle is: $area");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment