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 again and again.

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 Of Functions In C++ Programming Language




Basic 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 called 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

Now we are going to look at the first coding example in C++ programming language.




Now if we look at this above coding example, we have created a function named Hello() above the main() method. 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. It means that when the main() method will run, at the first line, Hello() method will be called and a message will be printed in the console screen that is Say hello to my first function. 

Now we will look at the second coding example.




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. 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 in 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() 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

Popular posts from this blog

How to run c project in visual studio community 2022

Nested Loops In C++ Programming Language

While Loop in C++ Programming Language