by Dinesh Thakur Category: Function and Pointer

Recall that a function call takes the form

func_ name ( arg_list )

where func _name is the name of the function being called and arg_list is a comma separated list of arguments. The number of arguments, their types and order must be in accordance with the function parameters specified in the function definition. When a function is called, the values specified in arg_list are passed to the function. The called function will usually use or process these values in some way.

A function may return a value. When it does, we can call that function from within an expression. If a function does not return a value or if we are not interested in the value returned, a function call takes the form of a C statement as in

func_name ( arg_list ) ;

The examples given below illustrate the various ways in which a function func that returns a value can be called. Note that an argument may be a constant, variable or expression.

x = func(3);          /* RHS of an assignment statement */

x = c + func(a+b);    /* as an operand in an expression */

printf("%d",func(b)); /* as an argument for another function */

func (a - b * c);     /*  ignore return value */

When a function is called, execution of the current function is suspended. The argument expressions (if present) are evaluated and their values assigned to the corresponding function parameters, and program control is transferred to the called function. The statements in the called function are then executed, starting from the first executable statement until a return statement is encountered or all the statements have been executed.

When the execution of the called function is complete, control is transferred to the calling function to the point from where the function was called. The return value, if any, is returned in place of the function call.

Advantages of Functions

There are several advantages in using functions. They are discussed below.

1. Functions allow the divide and conquer strategy to be used for the development of programs. When developing even a moderately sized program, it is very difficult if not impossible, to write the entire program as a single large main function. Such programs are very difficult to test, debug and maintain. The task to be performed is normally divided into several independent subtasks, thereby reducing the overall complexity; a separate function is written for each subtask. In fact, we can further divide each subtask into smaller subtasks, further reducing the complexity.

2. Functions help avoid duplication of effort and code in programs. During the development of a program, the same or similar activity may be required to be performed more than once. The use of functions in such situations avoids duplication of effort and code in programs. This reduces the size of the source program as well as the executable program. It also reduces the time required to write, test, debug and maintain such programs, thus reducing program development and maintenance cost.

3. Functions enable us to hide the implementation details of a program, e. g., we have used library functions such as sqrt, log, sin, etc. without ever knowing how they are implemented. However, although we need to know the implementation details for user-defined functions, once a function is developed and tested, we can continue to use it without going into its implementation details. Another consequence of hiding implementation details is improvement in the readability of a program. Proper use of functions leads to programs that are easier to read and understand.

4. The divide and conquer approach also allows the parts of a program to be developed, tested and debugged independently and possibly concurrently by members of a programming team. The involvement of several programmers, which is the norm in the development of a software project, reduces the overall development time.

5. The functions developed for one program can be used, if required, in another with little or no modification. This further reduces program development time and cost.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles