15.6k views
5 votes
Write a Scheme function called "sum" which takes an input function func and a nonnegative number n and outputs the value

func(1) + func(2) + .. + func(n)

User Piero
by
5.3k points

1 Answer

3 votes

Answer:

function sum(number) {

if (number == 1) {

return 1;

}

return number + sum(number -1);

}

Step-by-step explanation:

This is a recursive function, it means that is a function that calls itself for example: if you call the function with sum(5) the process is :

sum(5)

|______ 5 + sum(4)

|_______ 4 + sum(3)

|______ 3 + sum(2)

|_____2 + sum(1)

|_____ 1

the result is 1+2+3+4+5 = 15

User WithoutOne
by
5.2k points