How to create your own functions - create a program named


Pass Task 2.4: My Functions

Overview

Procedures are a great tool for capturing the instructions needed to perform a task, but some- times you need to be able to capture the instructions needed to calculate a value. Using functions you can now create artefacts to encapsulate the steps needed to calculate a value.

Purpose: Learn how to create your own functions.

Task: Use the following instructions to implement a function and use it to calculate some values based on user input.

Instructions

In programming, functions are used to calculate a value. They are very similar to procedures, with the added feature that they return a result when they end. For example, the following program uses the Pi, Sqr and Round functions to calculate the area of a circle.

program CircleArea;
uses TerminalUserInput;

procedure Main();
var
cirRadius, cirArea: Single; roundedValue: Integer;
begin
cirRadius := ReadInteger('Enter a radius: '); cirArea := Pi() * Sqr(cirRadius); WriteLn('Circles area is ', cirArea:4:2); roundedValue := Round(cirArea);
WriteLn('Which rounded off has a value of ', roundedValue);
end;

begin
Main();
end.

In this program we could create a CircleArea function that calculates the area of a circle. That would mean that we could use that function any time we wanted to get the area of a circle.

Designing a function is just like designing a procedure. One way of approaching it is to think that someone has asked you to perform the calculation. Putting yourself in that position will help you think about the data the function will need to be given, and the data it will return. In this case you need to be told the radius of the circle, and you then return the area. The radius will be a number, and could have a fractional part so you use the Single data type. The result will then be a number and could have a fractional part, so it is also a Single. This gives you the functions prototype - which is all the details about the functions name, its parameters, and re- turn type.

The body of the function will then calculate the area of the circle, from the radius parameter. It will then set this as the result of the function. The value in the result variable is returned to the caller when the function ends.

function CircleArea(radius: Single): Single; var
area: Single; begin
area := Pi() * Sqr(radius); result := area;
end;

You can then call the CircleArea function in Main, and store the result of calling the function into Main's cirArea variable.

procedure Main();
var
cirRadius, cirArea: Single; roundedValue: Integer;
begin
cirRadius := ReadInteger('Enter a radius: '); cirArea := CircleArea(cirRadius); WriteLn('Circle area is ', cirArea:4:2); roundedValue := Round(area);
WriteLn('Which rounded off has a value of ', roundedValue);
end;

For this task you can choose to implement one of the following three programs:

- Air Speed Velocity: Calculate the air speed velocity of an unladen swallow.

- Compound Interest: Calculate how much interest you get on an investment.

- Distance Travelled: Calculate the distance travelled from an initial velocity, acceleration and time.

Instructions for each of these programs follow. Once you have completed the task, submit your code and a screenshot of the Terminal with the output of the program.

Option 1: Air Speed Velocity Instructions

For this task you will create a program to answer the age old question "What is the airspeed velocity of an unladen swallow?".

The air speed velocity of a bird can be calculated using an equation based on the Strouhal Number (also see). From this information we can determine that the air speed velocity of a bird (U) can be calculated from the frequency (f) at which the bird beats its wings multiplied by the amplitude (A) of each wing stroke, divided by the Strouhal Number (s). This is shown in the following equation:

U = (fA)/s

1. Create a program named AirSpeed, which will read and write values from the Terminal (using TerminalUserInput).

2. Declare a constant for the Strouhal Number with the value 0.33. In the code use UPPER- CASE for the constant. e.g.: const STROUHAL_NUM = 0.33;

3. Declare a function named AirSpeedVelocity. This function will have parameters for fre- quency and amplitude - both of the Double type, and it will return a Double value.

4. Main will write out 'African Swallow', and calculate the air speed based on a frequency of 15hz and amplitude of 21cm, and write the resulting velocity to the console.

5. Main will write out 'European Swallow', calculate the air speed based on a frequency of 14hz and an amplitude of 22cm, and write the resulting velocity to the console.

6. Main should then ask the user to enter a frequency and an amplitude and print out the air- speed based on the values entered.

Compile and run the program and check that you are getting the correct values, then prepare it for your portfolio.

Option 2: Investment Calculator

With compound interest, interest is paid on the principal and any interest received during the period of the investment. For example, if you invest $1000 at 15% interest then in the first year you will receive $150 interest. This interest is then added to your investment, and in the sec- ond year you will receive $172.50 interest (15% of the $1150).

The following formula can be used to calculate the amount of interest an investment will re- ceive over a period of time.

I = P (1 + i)n - P

1. Create a program named InvestmentCal, which will read and write values from the Terminal (using TerminalUserInput).

2. Declare a function named CompoundInterest. This function will have parameters for principal, years (both integers) and rate (a Double) and it will return a Double value.

3. Main will write out 'Bank A', and calculate the interest based on a principal of $1000, 3 years, at 3.5% (0.035).

4. Main will write out 'Bank B', and calculate the interest based on a principal of $1000, 3 years, at 4.5% (0.045).

5. Main should then ask the user to enter a principal and rate, and output the interest for a 5 year investment.

Compile and run the program and check that you are getting the correct values, then prepare it for your portfolio.

Option 3: Distance Calculator

It is possible to determine the distance travelled given an initial velocity, acceleration, and time. For example, if you start with a velocity of 8m/s and accelerate at 0.1m/s2 for 5 minutes (300 seconds) then you will have travelled 6900 meters.

The following formula can be used to calculate the distance from initial velocity (v) acceleration (a) and time (t).

D = vt + 0.5 at2

1. Create a program named DistanceTravelled, which will read and write values from the Terminal (using TerminalUserInput).

2. Declare a function named Distance. This function will have parameters for initial velocity, acceleration and time (all Double values), and it will return a Double value.

3. Main will write out 'Distance 1', and calculate the distance based on an initial velocity 0 m/ s, 120 seconds, with an acceleration of 0.07m/s2.

4. Main will write out 'Distance2', and calculate the distance based on an initial velocity 8.33
m/s, 120 seconds, with an acceleration of 0 m/s2.

5. Main should then ask the user to enter initial velocity, time, and acceleration, and output the distance for these values.

Compile and run the program and check that you are getting the correct values, then prepare it for your portfolio.

Attachment:- 2.4P-resources.zip

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: How to create your own functions - create a program named
Reference No:- TGS01401885

Expected delivery within 24 Hours