Followers 0

# Function (not a specific language)

## 8 posts in this topic

Hi all, I have written a program with a function and I want to know if it all looks okay.

"Design a function that accepts an object's falling time (in seconds) as an argument. The function should return the distance in meters that the object falls [ distance = 1/2(gravity)(time^2) ]. Design a program that calls the function in a loop that passes the values 1 through 10 as arguments and displays the return value."

Constant Real GRAVITY = 9.8

Module main( )

Declare Real distance, time, counter

For counter = 1 to 10

Set time = counter

Set distance = fallingDistance(time)

Display "Object falls", distance, "meters."

End For

End Module

Function Real fallingDistance(Real seconds)

Return 0.5 * GRAVITY * seconds^2

End Function

##### Share on other sites

If you have time or care to help, I'd also be curious to know if I am on the right track with a different problem, also using functions. The question tells you the program should prompt the user to enter five test scores, display a letter grade for each score, and then display the average score. The two functions to use are calcAverage (Accept five test scores and return average) and determineGrade (accept test score and return a letter grade as a string).

I don't know if I am making it too complicated or if what I have is close to the right answer!

Module main( )

Declare Real test1, test2, test3, test4, test5, avg

Display ?Enter test one score: ?

Input test1

Display ?Enter test two score: ?

Input test2

Display ?Enter test three score: ?

Input test3

Display ?Enter test four score: ?

Input test4

Display ?Enter test five score: ?

Input test5

Set avg = calcAverage(test1, test2, test3, test4, test5)

Display ?Average is: ?, average

End Module

If value < 60 Then

Else If value < 70 Then

Else If value < 80 Then

Else If value < 90 Then

Else If value <= 100 Then

End If

End Function

Function Real calcAverage(Real num1, num2, num3, num4, num5)

Declare Real result

Set result = (num1 + num2 + num3 + num4 + num5) / 5

Return result

End Function

##### Share on other sites

Both look ok, all in all, except:

First code:

1) Variable time is unnecessary.

Second code:

1) You've overlooked that determineGrade() should return String type. Currently it incorrectly wants to return Real.

2) You could do with just one grade variable in main(). Although that's premature optimization, strings are expensive memory in just about any implementation down the road.

##### Share on other sites

What would you have done if the program required you to input 100 instead of 5 scores? Repeat the same code 100 times with 100 different variables? Create an "average" function that takes 100 arguments as parameters?

What would you have done if the program required you to input as many scores as the user wishes? Create a different version of the program for every possible number of inputs?

Don't repeat the same code over and over. Make the computer do the repetitive boring jobs for you, not the reverse. Isolate the code that stays the same in functions, and the parts that change in function parameters. Change your variables and ifs to collections and loops.

1 person likes this

##### Share on other sites
Real num1, num2, num3, num4, num5

What is an array and why are they used?

##### Share on other sites

Thanks for the link. We haven't learned arrays in the book yet though!

##### Share on other sites

What would you have done if the program required you to input 100 instead of 5 scores? Repeat the same code 100 times with 100 different variables? Create an "average" function that takes 100 arguments as parameters?

What would you have done if the program required you to input as many scores as the user wishes? Create a different version of the program for every possible number of inputs?

Don't repeat the same code over and over. Make the computer do the repetitive boring jobs for you, not the reverse. Isolate the code that stays the same in functions, and the parts that change in function parameters. Change your variables and ifs to collections and loops.

Thank you for the help!

##### Share on other sites

Both look ok, all in all, except:

First code:

1) Variable time is unnecessary.

Second code:

1) You've overlooked that determineGrade() should return String type. Currently it incorrectly wants to return Real.

2) You could do with just one grade variable in main(). Although that's premature optimization, strings are expensive memory in just about any implementation down the road.

## Create an account

Register a new account