Functions In C++ Programming Language
Functions In C++ Programming Language
Today we are going to look at the use of functions in C++ programming language. We use functions to repeat or reuse some piece of code in our program. So a function in C++ is a reusable block of code that executes when it is called. Functions are the most important building blocks in C++, or any other programming language. These funtions allow the developers to divide or break a large program into smaller and manageable segments or parts. With the help of these functions we can reuse the same lines of code again and again or multiple times without need to write these lines of code multiple times.
Each and every function is used to perform some specific task. These functions are also referred as procedures or subroutines in different programming languages. We use these functions to achieve modularity in our program. Modular programming means breaking program into smaller independent parts (modules) so it is easy to develop, understand, and maintain a program.
Basic syntax and working of functions In C++ programming language
void MyFunction(){ // write coding part here. }
Working Of Functions In C++ Programming Language
Now we will look at basic working of functions in C++ programming language. We use functions to implement some functionality. When we create a function the first thing that we do is to declare a function like this
void add() { // write some piece of code here. }
In the above coding example we have declared a function with return type void and the name of function is add() followed by round brackets. Now always remember that after declaring and creating a function body we can use the function only by calling it. Calling a function means where ever you want to use a function you can simply write its name with round brackets i.e, add(); This is known as function calling, so at this point the function will perform its functionality according to your requirement or program logic. Suppose if in this function add() we write the code to print the word addition like this
void add() { cout << "addition"; }
Then where ever i call this function named as add() in my code, it will simply print the word addition at that point in the console screen.
Simple Programming Examples Of Functions In C++ Programming Language
Function coding example 1
#include<iostream> #include<conio.h> using namespace std; void Hello() { cout << "Say hello to my first function."; } void main() { Hello(); _getch(); }
Explanation of coding example 1
Now if we look at this above coding example, we have created a function named Hello() above the main() method.
void Hello() { cout << "Say hello to my first function."; }
Now in this Hello() method we have simply printed a message that is Say hello to my first function.
If we look at the main() method we have called this Hello() function at the very first line of the main() method.
void main()
{
Hello();
_getch();
}It means that when the main() method will run, at the first line, Hello() method will be called and a message will be printed to the console screen that is Say hello to my first function.
Function coding example 2
#include<iostream> #include<conio.h> using namespace std; void Add() { int Number1, Number2, Result; cout << "Enter first number = "; cin >> Number1; cout << "Enter second number = "; cin >> Number2; Result = Number1 + Number2; cout << "Sum of entered numbers = " << Result; } void main() { Add(); _getch(); }
Explanation of coding example 2
In the second programming example of functions we created a method, named as Add() with void return type it means that this method will return nothing.
void Add() { int Number1, Number2, Result; cout << "Enter first number = "; cin >> Number1; cout << "Enter second number = "; cin >> Number2; Result = Number1 + Number2; cout << "Sum of entered numbers = " << Result; }
Now in this method we are taking two numbers as input and then adding their sum and put the sum value to the variable named result. And at last show the the result of addition of two entered numbers to the console screen. So this method will add two numbers and show their result.
Now in the main() method at the very first line we are calling a method Add()
void main()
{
Add();
_getch();
}so at this point we will get a message to enter the first number and after entering the first number a message will be shown to enter the second number and the rest of the code i have already explained above. So in this way, the program is executed successfully.
Congratulations! like always we have successfully implemented functions in C++ programming language.
Comments
Post a Comment