Subroutines
As you have seen, a subroutine is a language construct that allows you to split a large task into smaller pieces. In the previous example, you implemented a subroutine that does the same thing every time it is called: outputs the same string to the console, and then waits until the user presses Enter. This case is rarely useful. There are many cases when you would like the subroutine to do the same operation, but on different data. In this example, we would like to output a different string when the user has entered an incorrect value and the program will terminate abnormally, and a success message when the task of summing the array elements is successful. This is achievable through the use of subroutines with arguments, as illustrated ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access