Question

Respond to the following in a minimum of 175 words:    It is important to program...

Respond to the following in a minimum of 175 words: 

 

It is important to program your code efficiently. Efficient code does not include duplicate code that performs the same procedure. Functions are helpful in making code modular. How can functions reduce the amount of written code and make code easier to read/follow?


Provide a code example that supports your comments.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Functions makes the program more efficient and readable. The following are key points regarding function:

  • By using the functions, it is not required to do a same task repeatedly. Function handles the repeated tasks so that efficiency of the program can be improved.
  • Function reduces the line of code. More the lines in the code, more the time program will take to execute. Function helps in reducing the line of code, so that program can be understand easily and can be run in a optimal time.
  • Function takes the inputs as parameters and gives the efficient output accordingly. They makes the code efficient.
  • Function also returns the values as per the requirements.
  • Normally, it is required to write the segments of functional code repeatedly, when it is required more than once in a program, but when using function it is only required to call the function in order to use the functionality.
  • For example, we need to add value 3 times in a program. we simply make the Sum() function and call them as per the requirements. This reduces the running time and writing of code time.
  • ---------------------------------------------------------------------------------------------------------------------------------------

Example: This example is concerned with CORAL language.

Without using Function:

integer factorial

integer i

integer n

factorial = 1

n = 3

for i= 1; i <= n; i = i + 1

   factorial = factorial* i

Put factorial to output

Put "\n" to output

factorial = 1

n = 4

for i = 1; i <= n; i = i + 1

   factorial = factorial * i

Put factorial to output

factorial = 1

n = 5

for i = 1; i <= n; i = + 1

   factorial = factorial * i

Put factorial to output

Put "\n" to output

We have used 3 different segments to calculate factorial of 3, 4, and 5

Same example using Function:

Function fact(integer n) returns nothing
integer factorial
integer i
factorial = 1

for i = 1; i <= n; i = i + 1
factorial = factorial * i
Put factorial to output

Function Main() returns nothing
fact(3)
fact(4)
fact(5)

Conclusion:

In second example, we just called the fact function with different arguments in order to get the resukt of that number. It is clearly seems that, the code becomes efficient.

Add a comment
Know the answer?
Add Answer to:
Respond to the following in a minimum of 175 words:    It is important to program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT