Question

In: Computer Science

Discuss Exercises #1 on page 171: Anne complains that defining functions to use in her programs...

Discuss Exercises #1 on page 171: Anne complains that defining functions to use in her programs is a lot of extra work. She says she can finish her programs much more quickly if she just writes them using the basic operators and control statements. State three reasons why her view is shortsighted. Explain why functions are useful in structuring code in a program.

Solutions

Expert Solution

Lets first write some general statements as to why functions are important.
- Function provide readability in the code.
- Functions avoid redundancy in the code.
- Program is broken down in sub parts of functions which is easy to understand by any new joiner.
- Name of function directly refers to the work that a function does (convention). So, just by looking at function , we can come to know about the overview of code it computes.


1.) Lets take an example where we need to write a code that computes average , max , min of students marks based on user input. Enter 1 - > Avergae. 2 -> Max. 3 -> Min 4-> exit

Consider the below steps in case no function is involved
- Show menu on screen by writting print statements.
- Take input from user

- Open and read file
- Does the calculation

- These steps are for one user. This menu has to be shown until user exits. In this case, we have to keep on writting the print statements for the menu. Number of print statements will be N where N is number of times user does an action.



Consider the below steps is case function is involved
- Call function to show menu
- Call funtion to take user input and return a value.
- Return contents of file from a function.
- Calls another function that computes the result and return it to the caller.

Here, we have to just define all the cases only ONCE. In every case, we just call the method and get the result.


2.)

Modifications - > In case we want to debug the code and we know there is some error in a portion of code, it is always easier to maintain the code if one logical set of code is seperated from another set of code.

3.)
Code growth -> It is okay if the lines of code of a program are 10-100 or even in thousands. But, this is not a practical case. In a real life project, the lines of codes are in lakhs. To maintain that huge amount of code, we need some logical segregated approach to identify any issues or make any code changes.

4.)
Reusability -> If same snippet is required at some other place in a project/program, we need to write that again. So, better have functions. Just pass a value to it and get the result without having to write code again.



*Kindly upvote if this helped


Related Solutions

) Use functions and arrays to complete the following programs. Requirements: • You should use the...
) Use functions and arrays to complete the following programs. Requirements: • You should use the divide-and-conquer strategy and write multiple functions. • You should always use arrays for the data sequences. • You should always use const int to define the sizes of your arrays. a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally,...
Review Exercises questions 1-4 page 13 REVIEW EXERCISES: 1. Why are you interested in international economics?...
Review Exercises questions 1-4 page 13 REVIEW EXERCISES: 1. Why are you interested in international economics? What is motivating you? How are your interests, major, or profession affected by the world economy? 2.   What are the four windows on the world economy? 3.   What is the difference between trade in goods and trade in services? 4. What is the difference between international trade and foreign direct investment?
Use the following information to answer the next six exercises also using excel functions for the...
Use the following information to answer the next six exercises also using excel functions for the answers: Yoonie is a personnel manager in a large corporation. Each month she must review 16 of the employees. From past experience, she has found that the reviews take her approximately four hours each to do with a population standard deviation of 1.2 hours. Let Χ be the random variable representing the time it takes her to complete one review. Assume Χ is normally...
Please in C++ (Carrano) Use Exercises 3. Page 391. Chap 13 to. As the main program...
Please in C++ (Carrano) Use Exercises 3. Page 391. Chap 13 to. As the main program for the LinkedQueue structure p. 403 Chap 14. the linkedQueue structure is added. 3. What is the output of the following pseudocode, where num1, num2, and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " "...
rite a 2-3-page paper discussing the structures and functions of the skin. Paper must discuss: Discuss...
rite a 2-3-page paper discussing the structures and functions of the skin. Paper must discuss: Discuss what happens to the skin during the aging process and what kind of special care must be given to the skin and hair of older clients. Make a list of preventive measures that can slow the aging process. What are the skin’s structures and function? Include in your paper the following case scenario: Delgattio has psoriasis. Why would rapid epidermal proliferation cause thick, silvery,...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide the following inputs and outputs for each function/program: EncryptCaesar Two inputs: A string of the plaintext to encrypt A key (a number) ▪ For the Caesar cipher: This will indicate how many characters to shift (e.g. for a key=3, A=>D, B=>E, ..., X=>A, Y=>B, Z=>C). Note that the shift is circular. One output: ◦ A string of the ciphertext or codeword DecryptCaesar Two inputs:...
For the following exercises, use the definition for the derivative at a pointto find the derivative of the functions. f(x) = −x2 + 4x + 7
For the following exercises, use the definition for the derivative at a pointto find the derivative of the functions.f(x) = −x2 + 4x + 7
For the following exercises, use the definition for the derivative at a pointto find the derivative of the functions. f(x) = −x2 + 4x + 7
For the following exercises, use the definition for the derivative at a pointto find the derivative of the functions.f(x) = −x2 + 4x + 7
Question 1 Discuss the key functions of an operating system, and how these functions make it...
Question 1 Discuss the key functions of an operating system, and how these functions make it possible for computer applications to effectively utilize computer system resources. Compare and contrast the Windows API and the POSIX API and briefly explain how these APIs can be applied in systems programming. Write a C/C++ program to create a file in a specified folder of the Windows file system, and write some text to the file. Compile and run the program and copy the...
For the following exercises, describe the end behavior of the graphs of the functions. f(x) = −5(4)x − 1
For the following exercises, describe the end behavior of the graphs of the functions.f(x) = −5(4)x − 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT