In this lesson, we’ll look at some functions and decide on their prototypes. We’ll be looking at what should be the inputs and outputs of these functions, without discussing the internal details (that we will discuss in the upcoming lessons).

Some examples of designing functions

Let’s look at some example functions and their respective prototypes.

Factorial computation

Factorial is a mathematical quantity that measures the number of possible ordered arrangements. This is shown below:

n!=n×(n−1)×(n−2)×...×2×1n! = n \times (n-1) \times (n-2) \times ... \times 2 \times 1

For example, the factorial of 5 is calculated as 5×4×3×2×1=1205 \times 4 \times 3 \times 2 \times 1 = 120.

From this definition, we can see that both the input and output of this function should be integers because, when we multiply integers, we get an integer. The image of the factorial function as a black box (without focusing on the actual code implementation of the function) can be seen below.

Here, we see that the factorial() function should have only one parameter of type int, and its output would also be int.

The prototype of the factorial() function will then be as follows:

int factorial(int num);

It could also simply be the following:

int factorial(int);

Euclidean distance

Distance is a mathematical quantity that measures the distance between two points.

The distance between any two points is given by the following:

D = (x2−x1)2+(y2−y1)2\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}

For example, when calculating the distance between the two points, A (5,4) and B (10,15), the distance (D) would be as follows:

D = (10−5)(10−5)+(15−4)(15−4)\sqrt{(10-5)(10-5) + (15-4)(15-4)} = 12.0812.08

From this, we can see that the input of this function should be two points (where every point has an x coordinate and a y coordinate). Hence, it should have four inputs, whether int or float, and the output would be a real value (which means a float or double, depending on how precisely we want to calculate the value in terms of the C++ language).

The reason we have the return type float or double of the distance() function is that there is a square root in the distance formula. So, if the number is 50, , 50\sqrt{50} is 7.07106781187. We are most likely to get answers in decimals.

The image below shows two possible prototypes of the distance() function.

The image above shows two modular representations of the distance() function where the first one works on integers and the second on doubles.

The prototype of the first distance() function will be as follows:

double distance(int p1x, int p1y, int p2x, int p2y);

The prototype of the second distance() function will be as follows:

double distance(double p1x, double p1y, double p2x, double p2y);

We can call this the distance() function wherever in the code we need to use this module.

Integer distance square

Integer distance square is a mathematical quantity that measures the square of the distance between two points with integer coordinates.

For example, the square distance between points A(5,4) and B(10,15) would be

D2=(10−5)(10−5)+(15−4)(15−4)=146D^2 = (10-5) (10-5) + (15-4) (15-4) = 146

In general, the formula is as follows:

D2=(x2−x1)2+(y2−y1)2D^2=(x_2-x_1)^2+(y_2-y_1)^2

Distance square function

Question

a) What should be the data type of the output of the integerDistanceSquare() function that calculates the square of the distance between two points? Also, what is the prototype of the function?

b) What should be the data type of the output of the realDistanceSquare() function that calculates the distance between two points whose coordinates are floating values? Also, what is the prototype of the function?

Show Answer

Power computation

Power is a mathematical quantity that measures the power of a number. For example, 232^3 is calculated as 2×2×2=82 \times 2 \times 2 = 8.

We can generally write it as NkN^k, where kk is a number that tells us how many times NN is to be multiplied by itself.

For writing the power() function, we will pass only two integers (one for NN and one for kk) as parameters, and the function requires that it should generate an integer as the output (equal to NkN^k).

The power() function can be seen in the image below.

The prototype of the power() function will be as follows:

int power(int n, int k);
Q

What if the data type of n is double? In that case, what should be the return type of the power() function?

______ power(double n, int k);
A)

int

B)

double

Square root

We’ll see two variations of square root and write their function prototypes.

Non-integer square roots

Say we take the square root of 4949. Then 49=7\sqrt{49} = 7, which is an integer and 7∗77 * 7 equals 4949 (a perfect square). However, the square root can also be an irrational numbera real number that cannot be expressed as a perfect ratio p/q or complete floating pointing representation. Say we take the square root of 2626. Then 26\sqrt{26} = 5.099019513592785… which is a floating value (an approximation as 26).

This shows that the square roots of numbers can be either integers or floating values, depending on the number.

Integer square roots (approximation)

Integer square root is the quantity (an approximation) consisting of only the integer part. For example, the integer square root of 26 yields 5.

Look at the two functions sqrt() and integerSqrt() in the image below.

Question

What are the function prototypes of the two functions (sqrt() and integerSqrt()) shown in the image above?

Show Answer
  • sqrt(): This takes an integer and returns the approximation of the square root.
  • integerSqrt(): This returns only the integer part of the square root.

Number comparisons

We want to find a minimum of three integer numbers. Say we have a function called minimum() in which we pass three numbers, num1, num2, and num3, and need to figure out which of the three is the minimum. For example, if we have three numbers, 9, 7, and 3, then the minimum is 3. So, the prototype of the function should have three values as parameters, int num1, int num2, and int num3, and the output should also be an integer int min.

Similarly, we can have the maximum() and middle() functions for comparing the three integer numbers, num1, num2, and num3, and figure out which of the three is the maximum and middle number.

The three functions would look like this:

All three functions, minimum(), maximum(), and middle(), take three integer inputs each and return the minimum, maximum, and middle elements of the three of its inputs.

The prototypes of the three functions will be as follows:

int maximum(int num1, int num2, int num3);

int minimum(int num1, int num2, int num3);

int middle(int num1, int num2, int num3);

Type of a letter

Given a character, char symbol, we would like to find whether the given alphabet is capital, small, or neither.

We will make the function letterType(). Let us make a convention that it should return 1 for a capital letter, 2 for a small letter, and 3 if the input entered is neither a capital nor small letter (for example, any non-alphabet character like '%', '0', '$', or '5'). Since 1, 2, and 3 are integers, the return type of this function will be an int.

For example, suppose we pass the alphabet 'A' (a capital letter) as a parameter. In that case, the output of the letterType() function should be 1. If we pass 'a' as a parameter, then the output will be 2. If we pass a symbol like '*', which is not an alphabet, then it should return 3.

Let us also make two utility functions, isCapital() and isSmall(), to determine whether the character symbol is a capital or small English letter. Note that both the above functions are Boolean functions because these functions will be returning either true or false. Hence, their return type will be bool.

Question

What are the function prototypes of the three functions (letterType(), isCapital(), and isSmall()) shown in the image above.

Show Answer

Multiple of each other

In this function, we have to tell whether the given numbers are multiples of each other or not.

It has 4 cases as follows:

  • It returns 1 if n1 divides n2.

  • It returns 2 if n2 divides n1.

  • It returns 3 if they both divide each other.

  • It returns 4 if none is the divisor of the other.

It should look like the following:

Question

What are the function prototypes of the function (isMultiple()) shown in the image above.

Show Answer

We have designed several functions’ prototypes. The above exercise was meant to equip you to think of designing subproblems independently. Now, in the upcoming lessons, we will be implementing these functions.