Chapter Overview
Learn what we will be covering in this chapter.
“Divide and conquer must become define and empower.”
In this chapter, we will learn about the divide-and-conquer approach using functions.
We’ll revisit some of the previous problems and solve them using functions this time. This will help us better appreciate the difference between the solutions (with and without the functions).
Below is an animation that displays the working of the add()
function. Notice the flow of execution and the steps shown in the following animation. Here, the three variables are initialized and then passed as parameters to the add()
function. After the add()
function executes (the right-side window containing the function’s implementation), the function returns the sum
, which is stored inside the result
variable. In the end, the result
variable is updated in the “Watches” section as well.
More importantly, using functions would help us appreciate the use of an integrated debugger (GDB) when running our programs.
We will be able to:
- Understand the flow of the program execution.
- Run the program to a certain point to understand the code by setting the breakpoints on specific functions or lines. Breakpoints can be used to halt the execution of the program at a specific spot.
- Examine the values of the variables (under the local variables section).
- Change the values of variables during program execution.