-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Thursday, 7 May 2015

Functions

Functions

What are functions?


You are familiar with this word in Mathematics already, but what is its purpose in C++?

Today I will explain functions in simple words. In general,a function is a block of code that performs the same action again and again.

Example from daily life


Consider that you went to a library in which there are thousands of books arranged in different shelves. Suppose you want to read to particular book. You take that book out of the shelf and read it for some time. After reading it, you put that book again in that shelf.

Now, remember this situation for the concept of "functions". Suppose you made a function for calculating factorial of a given number. You are writing a program of  200 lines. At the middle of this program, you want to calculate the factorial of a given number. So, you will simply call that function. No need to write the complete code for calculating factorial. You can use the same function again and again.

This is very much similar to the above situation. Whenever we want to read a particular book, we take it out of the shelf. In the same way, whenever we want to use a function, we call it by its name.

Advantages of functions:


There are many advantages of using a function:

* A function is easier to code.

* It is easier to modify.

* It is easy to debug.

* We can save programming time.

* Program size becomes less.


No comments:

Post a Comment