Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 7: Functions

Chapter 7: Functions

pp. 299-370

Authors

, Vidyalankar Institute of Technology
  • Add bookmark
  • Cite
  • Share

Summary

Overview

Creation and maintenance of the program becomes difficult when the lines of code in the program increase. As the size of the program increases, the code becomes more prone to errors and the debugging of the program becomes difficult. The line of code (LOC) is considered as one of the metrics to determine the logical complexity of the program in the software development life cycle. Any program is a sequence of instructions and there can be a situation where a subsequence needs to be repeatedly executed at different places in the program. Repeating the subsequence at each place will not only increase the LOC but also make the program bulky and hamper its readability, thereby making change management cumbersome. Imagine a situation wherein a certain operation (say performing addition) is repeatedly written in the program at 100 different places, and suddenly you realize that you need to perform subtractions instead of additions. Making this change is going to be difficult because you need to ensure that all the addition operations at 100 different places need to change to subtraction. And what if you miss one of them to change? Or what if you change something else by mistake in a rush to make these 100 changes? The problem is that there is a lot of duplication of code (redundancy of code), and hence making one change requires making multiple changes.

In C/C++, it is possible to split a large program into multiple independent pieces. Each independent piece is called a function or a method. A function can define a single operation or a sequence of operations. You can invoke a function whenever you need to execute the operations it defines. Hence, the idea is to define an operation only once within the function and invoke the same function at multiple places where you need to perform the operation. A function can have multiple inputs (called arguments) and generate a single output (called return value) as shown in Figure 7.1.

About the book

Access options

Review the options below to login to check your access.

Purchase options

eTextbook
US$110.00
Paperback
US$110.00

Have an access code?

To redeem an access code, please log in with your personal login.

If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.

Also available to purchase from these educational ebook suppliers