How to create and understand functions in C

Without a single function implemented in a C++ program, the code will not run. The most common function that is used when learning to programming in C++ is the “int main()” function. Functions in C++are known as a block of code that performs a specific task which can be reused multiple times in different code segments. Most programming languages like C++ contain many pre written set of functions which are kept in a library. We can also create our own functions to perform specialized tasks.

Creating Functions

The variable types you can use when you are creating your own functions are exactly the same variable types you use to declare identifiers such as int, double, void and long. The steps to create a function follows:

creating function steps

Example of creating our own functions: Programming a Quadratic equation solver 

For those who do not know what a quadratic equation is, they are equations used in graphing parabolas and the quadratic formula can be used to find the values when it hits the x axis. We are going to apply functions to code one.

quadractic equation

quadratic-equation graph

The first steps to creating a C++ program is including the header files and declaring “using namespace std”. Since we are calculating values of a quadratic equation, we need to include the header file, as it contains the square root function that we will use later in the code. In lines 13 – 17, we are declaring the function if you do not declare the function prototypes, the functions you created will not compile and run.

Notice that we are declaring void functions. The reason why we are using void functions is because we are not asking the complier to return a value. If a function you are implementing requires you to return a value, you cannot use a void function.

functions part 1

In lines 20 – 38, the enterValues function is now being implemented with instructions to obtain the values entered in by the user and then call the QuadracticCalculation function and execute that separate function with the values inputted in by the user.

functions part 2

Now we are implementing the Quadratic Formula in lines 40 – 85. Yikes, that’s a lot of code just to calculate two values. The reason why there there is a lot of code is because the quadratic formula has three conditions which the determinant is either greater, equal or less than zero, so for our function to work, we have to implement all three cases. As you can see in line 43, we are using the same variables, “double a, double b, double c”. These variables are parameters of the function and are only for private access internally within implementing the QuadracticCalcuation function.

functions part 3

functions part 4

The two functions we created, enterValues and QuadracticCalcuation have now been implemented. In lines 87 – 99, we are implementing the int main function as every single C++ program needs this line to run. Line 89, we are declaring three variables to be used in the enterValues function. These variables declared in the int main block are known as the actual parameters, which are values entered in by the user from the enterValues function, to calculate the quadratic roots.

functions part 5

Sample run of the Quadratic Equation solver

quadractic example run

Share this post