// Exercise 11.01 - user defined functions // // Aim: Understand how to define new functions and how to call // user defined functions // // The factorial of the number n is the product of the numbers from // one to n: 1* 2 * 3 * ... * n-1 * n. //* Write a function that uses a for loop to calculate the factorial. // The function should have one parameter n and return the value // of the factorial of n. Use the function to print the // factorial of 5, // the factorial of the factorial of 5 and // the factorial of 3. "\\Clear"; // Your code starts after this line print(factorial(5)); print(factorial(factorial(5))); print(factorial(3)); // Your code ends before this line function factorial(n) { result = 1; for(i=1; i<=n; i++) { result *= i; } return(result); } // Your code ends before this line // The code below is for automatically checking the result. Please ignore it! output = split(getInfo("log")); ok = ((parseInt(output[0]) == 120) && (output[1]=="6.689502913449124E198") && (parseInt(output[2]) == 6)); if (ok) showMessage("That's right. Great, you did it!"); else showMessage("Your result is wrong! Please check your macro and try again!");