[go: up one dir, main page]

0% found this document useful (0 votes)
22 views14 pages

Dart Programming (Oop)

The document provides an overview of functions in Dart programming, emphasizing their importance for maintainability, readability, and reusability. It covers defining, calling, and returning values from functions, as well as parameterized and optional parameters, including required named parameters. Additionally, it introduces lambda functions as a concise way to represent functions.

Uploaded by

micheal appiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

Dart Programming (Oop)

The document provides an overview of functions in Dart programming, emphasizing their importance for maintainability, readability, and reusability. It covers defining, calling, and returning values from functions, as well as parameterized and optional parameters, including required named parameters. Additionally, it introduces lambda functions as a concise way to represent functions.

Uploaded by

micheal appiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

DART PROGRAMMING

FUNCTIONAL PROGRAMMING
FUNCTIONS

 Function is a set of statements to perform a specific task. The


purpose of writing functions is to have maintainable, readable and
reusable code.
 Maintainable: Function organize the program into logical blocks of
code which can easily be maintained outside the main code.
 Readable: Since functions can be written separately and in blocks, it
makes it easy to read through your code.
 Reusable: Main purpose of functions is to apply DRY(Don’t Repeat
Yourself). This means that statements that are written inside block of
function can be used so many times at different areas of our code
when needed.
DEFINING FUNCTIONS

 A function definition specifies what and how a specific task would be


done. The syntax for defining a standard function is:
function_name( ){
// statements
}
 Note that function_name simply refers to the name of the function
and can be any name that you want. The ( ) is what really define the
functions and the { } indicates the block of the function.
 Whatever statements that goes into the { } brackets of the function
becomes a part of the function and can only be executed when the
function is called.
ACCESSING / CALLING FUNCTIONS

 Before a function can be executed you need to call that function. That means the
function has no effect in the project if it’s not called. The syntax below is how you call a
function that is already defined:
function_name( );
 From the above you can see we did not include the { } of the function, that is, because
we are only calling then function and not defining it.
 The function can be called anywhere in your code if your need it. Example:
void main( ){
function_name( ); // this means we’ve called the function
}
function_name(){ // also note that function_name can be any name like: myFunction, display
etc
print(‘Hello World’);
}
RETURNING VALUES IN FUNCTIONS

 Functions may also return value along with the control, back to the
caller. Such functions are called as returning functions. The syntax for
such functions are:
return_type function_name( ){
// statements
return value;
}
 return_type can be any valid data type such String, int, double, Map,
List etc
 The data type of the value returned must match the return_type.
 A function can only return one and only one value.
Cont’d

 Example of retuning functions:


void main( ){
print(test());
}
String test(){
return “Hello World”;
}
 In the example above, we have defined a function that has a return type of
String and the value it is returning is “Hello World”. Notice how the type of
the value matches the return type.
 As we called the function inside the print() function, the print functions
display “Hello World” as it is returned by the function test().
Parameterized Functions.

 Parameters are a mechanism to pass values to functions. Parameter


values are passed to function during invocation / call of the functions.
The number of values passed to a function must match the value of
the parameters defined. Below is the syntax of the function:
function_name(data_type parameter1, data_type parameter2, …){
}
 Each parameter has it’s own unique data type, that means every
parameter data type can be different from each other or can be same
data type but needs to be defined explicitly.
 The … means that we can have as many parameters that we want.
Cont’d

 Example of a parameterized function:


void main( ){
test_param(123, “this is a string”);
}
test_param(int n1, String s1){
print(n1);
print(s1);
}
 The above code defines a function called test_param which takes in two parameters,
that is, n1 which of data type int and s1 which is of data type String.
 Also you can choose not to specify the data type of the parameter as this can be
determined dynamically at the runtime of the code. However I recommend you
always specify the data type.
OPTIONAL PARAMETERS

 Optional parameters can be used when arguments(values passed to


functions) need not be compulsorily passed for a function’s execution.
A parameter can be optional by appending a question mark ? to it’s
name.
 There are three types of optional parameters:
 Optional Positional Parameters
 Optional named parameters
 Optional parameters with default values.
Optional Positional Parameter

 To specify optional positional parameters, we use the square [ ] and mark the
parameters with the question mark ? .
void testFunction(int param1, [int? optional_param1, String? optional_param2]){
// function
}
 Looking at the function defined above, it means that param1 is compulsory
parameter, whiles optional_param1, and optional_param2 are optional
parameters. This means we can choose to pass values to these parameters or
not. If not value is passed to them, they have a default value of null.
 Also note that since this is a positional optional parameter, we have to pass the
value according to how they position inside the function.
 Any function that has a return type of void means that the function does not
return any value.
OPTIONAL NAMED PARAMETERS

 For optional named parameters the parameter’s name must be specified


while the value is being passed. Curly brace { } can be used to specify
option named parameters.
void main( ){
namedParams(22, param2: 45, param3: “this is a string”);
}
void namedParams(int param1, {int? param2, String? param3}){
// statements
}
 In the above code, you can see when we invoked/called the function
namedParams we specify the name of the parameters that were optional,
that is , param2 and param3. This is because they are optional parameters.
Optional parameters with default
values.
 Function parameters can also be assigned values by default. However you can still pass
values to those parameters if you want.
function_name(param1, {param2 = default_value}){ }
 Example code:
void main( ){
test_param(123);
}
void test_param(int n1, {int? s1 = 12}){
print(n1);
print(s1);
}
 In the above example, we’ve added a default value to s1 parameter, by assigning 12 to it,
this means that even if we don’t pass value to it at the invocation, we can still have 12 in
s1. The default value changes when we pass a value to it.
REQUIRED NAMED PARAMETERS

 Functions that have named parameters can also be required instead of being an
optional parameters. The syntax below:
function_name(param1, {required param2, required param3}){ }
 Example code below:
void main( ){
testParam(122, param2: 34);
}
void testParam(int param1, {required int param2, String? param3}){
// statements
}
 Even though param2 is a named parameter because of the keyword required, it
means that a value must be passed to it, otherwise the code will throw an error.
LAMBDA FUNCTIONS

 Lambda functions are a concise mechanism to represent functions.


These functions are also called the Arrow functions. Below is the
syntax:
return_type function_name(parameters) => expression;
 Example code:
Void main( ){
printMsg();
print(test());
}
void printMsg( ) => print(‘hello’);
int test( ) => 123;

You might also like