A computer program is a sequence of instructions given to a computer to perform a particular job. Computer read those instructions one by one and executes them. A function or sub program is a program which is written for the possible use of another program. Let's take an example to understand the role of a function. Say, we have a program which can calculate the sum of area of triangles against their height and base. To perform this task a programmer has to write instruction code to calculate the area of each triangle and lastly adding them to find out the total. In such case a good programmer always writes a function which can find out the area of a triangle against its given height and base. Now in main program the programmer only has to call this function to find out the area of each triangle.
From the above example we see that functions can divide a program or software project into several modules. This leads several advantages in software development. These advantages can be summarized as,
Role of a function in a program is very simple; it processes few variables and output a result. The variables that will be processed may come from any global source (e.g. data from database, system information, etc.) or they may come from the program from where it is called. Recall the example of finding area of a triangle where the input variables, - base and height are came from the calling program. In such case these variables are called arguments of a function. Thus a function may take one or more arguments to implement its job.
After successfully completing its job a function returns the result of processing to the calling program. But there are several cases like printing a document or storing data into database, etc. there is nothing to return to the calling program; instead it returns a binary status bit (true or false) indicating that whether the job is successfully completed or not.
As I said earlier a function is a program which is written for possible use of another program. Here another program may be the function itself. In such case it will be known as recursive program.