Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?

# Function (not a specific language)

7 replies to this topic

### #1 bs819

bs819

Neowinian

• Joined: 16-September 13

Posted 14 October 2013 - 18:31

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

### #2 OP bs819

bs819

Neowinian

• Joined: 16-September 13

Posted 14 October 2013 - 18:41

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

### #3 +Phouchg

Phouchg

Random Oracle

• Tech Issues Solved: 2
• Joined: 28-March 11
• Location: Krikkit
• OS: VTOS 6.1.7601 x64

Posted 14 October 2013 - 19:03

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.

### #4 Andre S.

Andre S.

Asik

• Tech Issues Solved: 5
• Joined: 26-October 05

Posted 15 October 2013 - 02:21

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.

### #5 threetonesun

threetonesun

Neowinian ULTRAKILL

• Tech Issues Solved: 1
• Joined: 26-February 02

Posted 15 October 2013 - 02:41

Real num1, num2, num3, num4, num5

What is an array and why are they used?

### #6 OP bs819

bs819

Neowinian

• Joined: 16-September 13

Posted 16 October 2013 - 16:18

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

### #7 OP bs819

bs819

Neowinian

• Joined: 16-September 13

Posted 16 October 2013 - 16:18

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!

### #8 OP bs819

bs819

Neowinian

• Joined: 16-September 13

Posted 16 October 2013 - 16:19

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.