1. Write a function that takes two integers as parameters and returns their sum.
  2. Create a function that accepts a string and returns its length.
  3. Implement a function that takes a list of integers and returns the maximum value from the list.
  4. Write a function that takes an optional parameter (a default value of 0) and returns its square.
  5. Create a function that returns an anonymous function which adds a specified number to its input.
  6. Implement a function that takes a list of strings and returns a new list containing the strings in uppercase.
  7. Write a function that takes a list of integers and filters out odd numbers, returning only the even numbers.
  8. Create a higher-order function that takes a list of numbers and a function, then applies that function to each number in the list.
  9. Write a function that takes a list of integers and returns the average of those integers.
  10. Implement a function that takes a list of integers and returns a closure that can increment a counter based on the last value processed.

Solutions to Practice Questions on Functions in Dart
 

1. Write a function that takes two integers as parameters and returns their sum.

 

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

void main() {
    print(sum(3, 5));  // Output: 8 
    }

Explanation: The sum function takes two integers, a and b, as parameters and returns their sum.

2. Create a function that accepts a string and returns its length.

int getStringLength(String str) {
    return str.length;
}

void main() {
    print(getStringLength("Hello, Dart!"));  // Output: 12 
    }

Explanation: The getStringLength function takes a string str and returns its length using the length property.

3. Implement a function that takes a list of integers and returns the maximum value from the list.

int findMax(List<int> numbers) {
    return numbers.reduce((a, b) => a > b ? a : b);
}

void main() {
    print(findMax([3, 5, 7, 2, 8]));  // Output: 8 
    }

Explanation: The findMax function uses the reduce method to compare elements and find the maximum value in the list.

4. Write a function that takes an optional parameter (a default value of 0) and returns its square.

 

int square([int number = 0]) {
    return number * number;
}

void main() {
    print(square());      // Output: 0     
    print(square(4));    // Output: 16 
    }

Explanation: The square function has an optional parameter number with a default value of 0. It returns the square of the number.

5. Create a function that returns an anonymous function which adds a specified number to its input.

 

Function createAdder(int addend) {
    return (int value) => value + addend;
}

void main() {
    var addFive = createAdder(5);
    print(addFive(10));  // Output: 15 
    }

Explanation: The createAdder function returns an anonymous function that adds addend to its input. The closure retains access to addend.

6. Implement a function that takes a list of strings and returns a new list containing the strings in uppercase.

 

List<String> toUpperCaseList(List<String> strings) {
    return strings.map((s) => s.toUpperCase()).toList();
}

void main() {
    print(toUpperCaseList(["hello", "world"]));  // Output: [HELLO, WORLD] 
    }

Explanation: The toUpperCaseList function uses the map method to convert each string in the list to uppercase.

7. Write a function that takes a list of integers and filters out odd numbers, returning only the even numbers.

 

List<int> filterEvenNumbers(List<int> numbers) {
    return numbers.where((n) => n % 2 == 0).toList();
}

void main() {
    print(filterEvenNumbers([1, 2, 3, 4, 5]));  // Output: [2, 4]
     }

Explanation: The filterEvenNumbers function uses the where method to filter out odd numbers, returning only the even ones.

8. Create a higher-order function that takes a list of numbers and a function, then applies that function to each number in the list.

 

List<T> applyFunctionToList<T>(List<T> list, T Function(T) operation) {
    return list.map(operation).toList();
}

void main() {
    print(applyFunctionToList([1, 2, 3], (n) => n * 2));  // Output: [2, 4, 6] 
    }

Explanation: The applyFunctionToList function takes a list and a function, applying the function to each element using map.

9. Write a function that takes a list of integers and returns the average of those integers.

 

double calculateAverage(List<int> numbers) {
    return numbers.isNotEmpty ? numbers.reduce((a, b) => a + b) / numbers.length : 0.0;
}

void main() {
    print(calculateAverage([1, 2, 3, 4, 5]));  // Output: 3.0
     }

Explanation: The calculateAverage function computes the sum of the numbers using reduce and divides by the length of the list to get the average.

10. Implement a function that takes a list of integers and returns a closure that can increment a counter based on the last value processed.

 

Function createCounter(List<int> numbers) {
    int index = 0;
    return () {
        if (index < numbers.length) {
            return numbers[index++];
        }
        return null;  // Return null if the end of the list is reached     };
}

void main() {
    var counter = createCounter([10, 20, 30]);
    print(counter());  // Output: 10     
    print(counter());  // Output: 20     
    print(counter());  // Output: 30     
    print(counter());  // Output: null }

Explanation: The createCounter function returns a closure that increments the index each time it is called, allowing iteration through the list of numbers. If the end of the list is reached, it returns null.