Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Function-Test-2
Instruction: open a new project in Vscode called Exam3 and implement the folowing code Please implement your Answers with code
Instruction: open a new project in Vscode called Exam3 and implement the folowing code Please implement your Answers with code.

1) What is a function in Dart?
2) How do you declare a function in Dart?
Expand Down
3 changes: 3 additions & 0 deletions Samuel Chigozie/exam3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
3 changes: 3 additions & 0 deletions Samuel Chigozie/exam3/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
58 changes: 58 additions & 0 deletions Samuel Chigozie/exam3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Dart Functions Assignment

This repository contains the solution to an assignment focused on understanding Dart functions. The assignment covers various concepts related to functions in Dart programming language, along with practical examples.

## Assignment Questions

The assignment includes the following questions:
1. What is a function in Dart?
2. How do you declare a function in Dart?
3. What is the purpose of the main() function in Dart?
4. Explain the difference between a named function and an anonymous function.
5. What is a return type in Dart functions?
6. How can you pass parameters to a Dart function?
7. Describe the difference between positional and named parameters.
8. What is the significance of the arrow (=>) syntax in Dart functions?
9. How do you define default parameter values in Dart functions?
10. Explain the concept of optional parameters in Dart functions.
11. What is the purpose of the void keyword in Dart functions?
12. How can you define a function inside another function in Dart? What is this called?
13. What is a higher-order function in Dart?
14. Explain the difference between functions and methods in Dart.
15. How do you use the return keyword in Dart functions?
16. What is a function signature, and why is it important?
17. How can you make a function in Dart asynchronous?
18. Describe the use of the async, await, and Future keywords in asynchronous functions.
19. How do you call a function defined in another Dart file?

## Solution Overview

The solution provided in this repository offers step-by-step guidance on each question in the assignment. It includes explanations of concepts, code snippets, and practical examples to illustrate each topic.

## How to Use

To explore the solution to the assignment, follow these steps:

1. Clone this repository to your local machine using Git:

```
git clone https://github.com/your-username/dart-functions-assignment.git
```

2. Open the cloned repository in your favorite Dart editor or IDE (such as VSCode).

3. Navigate through the source code files to find answers to each question, along with explanations and code examples.

4. Experiment with the code examples provided to deepen your understanding of Dart functions.

## Contributing

If you find any issues with the solution provided or have suggestions for improvements, feel free to open an issue or submit a pull request. Contributions are welcome!

## License

This project is licensed under the [MIT License](LICENSE).

---

Feel free to customize the README content according to your preferences or additional information you may want to include!
30 changes: 30 additions & 0 deletions Samuel Chigozie/exam3/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
263 changes: 263 additions & 0 deletions Samuel Chigozie/exam3/bin/exam3.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
import 'hello.dart';

import '../lib/me.dart';

void main() {
print('Solution to Exam 3 Foundation Test');

hello('Samuel'); // example of calling a function defined in another Dart file
sayMyName(); // example of calling a function defined in another Dart file directory

int result = operateOnNumbers(3, 5, sum); // example of using a higher-order function
print('Result: $result');


print('Before calling the asynchronous function'); // example of calling an asynchronous function
// Calling the asynchronous function
myAsyncFunction();
print('After calling the asynchronous function');
}


/*

1. What is a function in Dart? A function in Dart is a reusable block of code that performs a specific task.
It allows you to organize your code into logical units, making it easier to read, understand, and maintain.
Here's a simple example:

*/

double calculateArea(double length, double width) {
return length * width;
}


/*

2. How do you declare a function in Dart? We declare a function using the `function` return type followed by the function's name, parameters (if any),
and the function body enclosed in curly braces `{}`. Here's a simple example:

*/
void greet() {
print('Hello, world!');
}


/*

3. What is the purpose of the main() function in Dart? The `main()` function in Dart is the entry point of a Dart program.
It is where the execution of the program begins. Dart programs must have a `main()` function as the starting point.
Check the main function in the first code snippet above

*/

/*

4. Explain the difference between a named function and an anonymous function.
A named function is a function that has a specific name assigned to it,
allowing you to call it by its name. An anonymous function, on the other hand, is a function without a name.
Anonymous functions are often used as arguments to other functions or as callbacks.

*/


/*

5. What is a return type in Dart functions?
The return type in Dart functions specifies the type of value that the function will return after its execution.
It can be any valid Data type or `void` if the function doesn't return anything. For example:

*/

int add(int a, int b) {
return a + b;
}

/*

6. How can you pass parameters to a Dart function?
You can pass parameters to a Dart function by including them inside the parentheses `()` in the function declaration.
Parameters are variables that store the values passed to the function when it is called. For example:

*/

void greeted(String name) {
print('Hello, $name!');
}

/*

7. Describe the difference between positional and named parameters.
Positional parameters in Dart are passed based on their positions in the function call.
Named parameters, on the other hand, are passed by specifying the parameter name along with the value.
Named parameters are enclosed in curly braces `{}` in the function declaration.

*/


/*

8. What is the significance of the arrow (=>) syntax in Dart functions?
The arrow `=>` syntax in Dart functions is used for concise one-line function bodies.
It is a shorthand way to write functions that contain a single expression.
The expression on the right side of the arrow is evaluated and returned as the result of the function. For example:

*/

int square(int x) => x * x;


/*

9. How do you define default parameter values in Dart functions?
You can define default parameter values in Dart functions by assigning a default value to the parameter in the function declaration.
If no value is provided for that parameter when the function is called, the default value will be used. For example:

*/

void greetWithName({String name = 'World'}) {
print('Hello, $name!');
}


/*

10. Explain the concept of optional parameters in Dart functions.
Optional parameters in Dart functions allow you to define parameters that are not mandatory to provide when calling the function.
They can be either positional or named parameters. Optional parameters are useful when you want to provide flexibility in function calls. For example:

*/

void greeting(String name, [String? greeting]) {
if (greeting != null) {
print('$greeting, $name!');
} else {
print('Hello, $name!');
}
}


/*
11. What is the purpose of the void keyword in Dart functions?
The `void` keyword in Dart functions is used to indicate that the function does not return any value.
Functions with a return type of `void` are called "void functions" and are used when the function does not need to return any specific value.
For example:

*/

void printMessage(String message) {
print(message);
}

/*

12. How can you define a function inside another function in Dart? What is this called?
This is called a "nested function."
Nested functions have access to the variables and parameters of the outer function, allowing for more modular and encapsulated code.
For example:

*/

void outerFunction() {
void innerFunction() {
print('Inside inner function');
}

innerFunction(); // Call the nested function
}

/*

13. What is a higher-order function in Dart?
A higher-order function in Dart is a function that takes one or more functions as arguments or returns a function as its result.
In other words, it operates on other functions by taking them as arguments or returning them as results.
Higher-order functions are a powerful concept in Dart, enabling functional programming paradigms.
example:

*/

int operateOnNumbers(int a, int b, int Function(int, int) operation) {
return operation(a, b);
}

int sum(int a, int b) {
return a + b;
}

// void main() {
// int result = operateOnNumbers(3, 5, sum);
// print('Result: $result'); // Output: Result: 8
// }


/*

14. Explain the difference between functions and methods in Dart.
In Dart, a function is a standalone block of code that performs a specific task, while a method is a function that is associated with a class or an object.
Methods are essentially functions that belong to a class and can access the properties and other methods of that class.

*/


/*

15. How do you use the return keyword in Dart functions?
In Dart functions, the `return` keyword is used to exit the function and return a value (if the function has a return type other than `void`).
It is followed by the value that is to be returned.
Once the `return` statement is executed, the function terminates and control is returned to the caller along with the specified value.

*/


/*
16. What is a function signature, and why is it important?
A function signature in Dart refers to the combination of a function's name, parameter types, and return type.
It uniquely identifies a function and defines its interface. Function signatures are important because they specify the contract of a function,
including the types of parameters it accepts and the type of value it returns.

*/


/*

17. How can you make a function in Dart asynchronous?
You can make a function in Dart asynchronous by using the `async` keyword in the function declaration.
An asynchronous function allows you to perform operations asynchronously, without blocking the execution of other code.
Asynchronous functions are often used when dealing with I/O operations or operations that may take some time to complete.
example:

*/

// An example asynchronous function
Future<void> myAsyncFunction() async {
print('Inside the asynchronous function');
await Future.delayed(Duration(seconds: 2));
print('After waiting for 2 seconds');
}

/*

18. Describe the use of the async, await, and Future keywords in asynchronous functions.
In Dart, the `async` keyword is used to mark a function as asynchronous, allowing it to use the `await` keyword.
The `await` keyword is used to pause the execution of an asynchronous function until a Future is resolved,
enabling sequential asynchronous code execution.
The `Future` keyword represents a potential value or error that will be available at some time in the future.

*/


/*

19. How do you call a function defined in another Dart file?
To call a function defined in another Dart file, you need to import that file using the `import` keyword.
Once the file is imported, you can call the function by its name as usual. For example:

*/

// Importing

//the file where the function is defined ==> import 'hello.dart';


// Calling the function from the imported file ==> hello('Samuel');
//check the main function in the first code snippet above
3 changes: 3 additions & 0 deletions Samuel Chigozie/exam3/bin/hello.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void hello(String name) {
print('Hello, $name!');
}
3 changes: 3 additions & 0 deletions Samuel Chigozie/exam3/lib/me.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void sayMyName() {
print('My name is Samuel!');
}
Loading