What is Recursion in C++ programming language

Image
 What is Recursion in C++ programming language Today in this blog post we will cover the concept of Recursion in C++ programming language for  beginners. So what is Recursion? And what is the use of Recursion in programming languages? We will cover the answer of these questions shortly. For now let us look at the basic introduction of Recursion. Recursion is a concept in which the function calls itself with in its own body, and breaks the complex problems into smaller and more manageable parts. So the first question was what is recursion? Basically Recursion is a method or technique which is used in programming languages and in this method the function calls itself. It means the function is called with in its own body. We will cover this concept with simple programming example which will make the concept of recursion crystal clear to the audience. The Second question was what is the use of Recursion in programming languages? Recursion simply breaks down complex problems or pro...

How to use functions in C programming Language

 Functions in C Programming Language






In programming languages, there is a concept of functions that we use to implement some functionality in our code. Functions help us reuse the code and enhance its performance, as they are written once and tested again and again.

In C  programming we can use functions to break the code into smaller pieces that are easily maintainable. A function is a piece of code that is enclosed in curly brackets, and a function only runs and executes when it is called within a main program in C programming language.

We can also pass data from our main program or function to any user-defined function. As we already discussed, the main advantage or benefit of functions is reusability. In functions, we can define the code once, and after that, we can use the function code many times, which enhances the reusability of the code.

We can also refer to functions as procedures or sub-routines in some programming languages. Basically when we run a C program the execution of the program starts from a function that is declared as void main(), compilers in programming languages look for this main() function and the execution of the program from this main function. 

In C programming we can say that different functions are combined to make a single program.


Syntax of declaring and defining a function in C programming:



As in the above code image the first line in which we write void Hello(); is called function declaration. In the other part of the code where we mentioned void Hello() again with opening and closing curly brackets {} is called defining a function or function definition.

After declaring and defining a function the next step is to call a specific function within a main program or main() function.


Example of a simple user-defined function that prints Hello World C Function!




Explanation of the function coding example:

This is a simple piece of code in which we have declared and defined a function named Hello(). This function simply prints a message "Hello World C Function!"

Now there are basically three steps taken, which are considered good practice, the first step is to declare a function, and after that define a function in which the code is written inside the function body.

The last step is to call a function inside the main program or main() function.

Like in the above code we already discussed about function declaration and definition previously. Now we are going to look at function calling or calling a function within main() function.

Inside the void main() function we call the function like this Hello(); this is a simple way to call a function within the main program by simply writing the function name with opening and closing round brackets and ending it with a semicolon (;) symbol.

And when we execute the program and call the function it simply prints the message "Hello World C Function!" 

Congratulations we have successfully implemented the function in our C Program.









Comments

Popular posts from this blog

What is Recursion in C++ programming language

Call by value and call by reference in C++ programming language

Nested Loops In C++ Programming Language