FloridaTech - College of Engineering
CSE1001 - LABORATORY


Lab Assignment 6: Subprograms / Functions

Subprograms are the conventional parameterized unit of programming. In Ada, subprograms fall into two categories: Functions and Procedures. This lab covers the subject of Functions in Ada.

Functions are called as components of expressions and return a value as part of the expression, whereas procedures are called as statements standing alone. Functions, and in general subprograms, are composed of a specification part and a body part . A Function generally takes arguments (parameters). Functions without arguments often return a constant value. This may be useful for inheritance purposes.

In this lab, we will try to implement Newton's formula to compute (x + y)n where x and y are Real numbers and n a positive Integer. Remember that Newton's Formula (also called Binome Formula) is :

where x and y are Real numbers (Float will be used to implement Real numbers) and n is a positive integers. The problem will be decomposed in three small problems to illustrate the devide-and-conquer strategy. Each of the problems consist in writing an Ada function. The student should also write test procedures to test the implemented functions.


Problem 0: Factorial Write an Ada function that takes as argument a non-negative integer n and returns factorial(n). Remember that factorial(n) = n! = n*(n-1)*...*1 if n>0 and factorial(0) = 0! = 1.


Problem 1: Binomial Coefficient Use problem 0 to write an Ada function that takes as argument two non-negative integers n and p where n >= p and returns choose(n,p) = n!/(p!(n-p)!).

Example of execution:
************************************************
********* Binomial Coefficients ****************
************************************************
Enter two positive integers :
7 5 --> choose(7,5) = 21.


Problem 2 : Fast Power In this problem, you are to write a function that takes as argument as real number x and a positive or null integer n then computes xn. Computing xn as x*x*x*....*x (n times) requires n multiplications which may be too slow in some applications. In general, the work of computer scientists (and sometimes computer Engineers also) is to find solutions to problems but still these solutions shouldn't use computer resources for a long time (relatively to the size of the problem to be solved). Therefore, in our problem, we will compute using only Log2(n) multiplication (asymptotically). As an exmaple, computing x1048576 requires 1048576 multiplications using the obvious algorithm but only 20 multiplications with the method proposed below.
xn = (x2)n/2 if n is even and x = x(n-1) * x if n is odd (and now (n-1) is even !).


Problem 3 (OPTIONAL): Binome's formula (Newton's formula) Using Functions written in Problems 1 and 2, write a function that takes as arguments two reals x, y and a postive integer n then computes (x+y)n using Newton's formula given above.

Example of execution:
*******************************************
********* Newton's formula ****************
*******************************************
Enter two reals : 5.0 3.0
Enter a positve integer : 2
The result is : 64