Add two Numbers using Function in C++
The below code is a simple example of adding two numbers in C++ using a function. The code uses the add function to add two numbers and the main function to call the add function and display the result on the console.
The code starts with the inclusion of the iostream header file, which is a standard input-output library in C++. The header file provides the functionality to perform input-output operations, such as reading from the keyboard and displaying results on the console. The using namespace std statement is used to avoid having to qualify each reference to a standard library entity with the namespace name.
Enter two numbers: 4 6 Sum of 4 and 6 is: 10
Next, we define the add function, which takes two integer parameters, a and b, and returns their sum as an integer. The function has a single line of code, which is return a + b, which calculates the sum of a and b and returns it to the calling function.
The main function is the starting point of the program, and all C++ programs must have a main function. In the main function, we declare three integer variables, num1, num2, and sum. We then use the cout function to display a message, "Enter two numbers: ", on the console, asking the user to enter two numbers. The cin function is used to read two integers from the keyboard and store them in the num1 and num2 variables, respectively.
Next, we call the add function and pass num1 and num2 as arguments. The result of the function is stored in the sum variable. Finally, we use the cout function to display the result on the console, "Sum of num1 and num2 is: sum". The endl statement is used to insert a newline character after the output and move the cursor to the next line. Finally, the main function returns the value 0, which indicates that the program has executed successfully. This is a simple example of how to use a function in C++ to add two numbers. Functions are an essential component of structured programming and provide a way to break down a complex problem into smaller, manageable parts. By using functions, you can make your code more readable, reusable, and maintainable.
Adding two numbers using a function in procedural programming is significant for several reasons: Modularity: Functions provide a way to break down a complex problem into smaller, manageable parts. By using functions, you can divide a large program into smaller, more manageable pieces, making your code more organized and easier to understand. Reusability: Functions can be reused throughout the program, making your code more efficient and less prone to errors. You can also easily modify a function if needed, and the changes will be automatically reflected in all the places where the function is used. Abstraction: Functions provide a level of abstraction between the underlying logic and the way it is used in the program.
This makes the code more readable and maintainable, as you only need to focus on the functionality provided by the function, rather than the implementation details. Better Debugging: Functions can be tested and debugged independently, making it easier to find and fix errors in your code. You can also add debugging information to a function to help you understand how it is functioning during runtime. Improved Code Readability: Functions make your code more readable by providing a clear and concise representation of the logic behind a specific task. By giving functions meaningful names and clear documentation, you can make your code easier to understand for others and for yourself when you come back to it later.
Next TopicC++ class to prevent object copies