C - Functions



A function in C is a block of organized, reusable code that is used to perform a single, related action. When the algorithm of a certain problem involves implementation of long and complex logic, it is broken into smaller, independent and reusable blocks known by different names in different programming languages such as a module, a subroutine, a function or a method. It is designed to perform a specific task that is a part of entire process. This approach towards software development is called modular programming.

Modular approach

In modular programming takes a top−down approach towards software development. The programming solution has a main routine through which smaller independent modules (functions) are called upon. Each function is a separate, complete and reusable software component. When called, a function performs a specified task and returns the control back to the calling routine, optionally along with result of its process.

Main advantage of this approach is that the code becomes easy to follow, develop and maintain.

Modular Approach

Above diagram shows how the process of salary computation is successively broken down to independent and reusable functions.

C offers a number of library functions included in different header files. For example, the stdio.h header file includes printf() and scanf() functions. Similarly, the math.h header file includes a number of functions such as sin(), pow(), sqrt() and more. These functions perform a predefined task and can be called upon in any program as per requirement. However, if you don’t find a suitable library function to serve your purpose, you can define one.

User−defined function

Writing a user−defined function involves the following steps −

  • Understand the purpose of the function − what do we want the function to do?
  • A function may need one or more data items to process. The data that the function needs from the calling environment are called arguments.
  • Inside the body of the function, one or more local variables may be needed for the processing.
  • Decide on the set of steps that the program will use to accomplish this goal. (The Algorithm)
  • After all the statements in the function are executed, the flow of the program returns to the calling environment. The function may return some data along with the flow control.

The following figure illustrates the function calling mechanism in C −

Function Calling Mechanism

In C, it is necessary to provide the forward declaration of the prototype of any function. The prototype of a library function is present in the corresponding header file. For a user−defined function, its prototype is present in the current program. The definition of a function and its prototype declaration should match.

Defining a function

A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −

  • Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Argument List − An argument (also called parameter) is like a placeholder. When a function is invoked, you pass a value as a parameter. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body − The function body contains a collection of statements that defines what the function does.

A function in C should have a return type. The type of the variable returned by the function must be the return type of the function. In the above figure, the add() function returns an int type.

Calling a function

To call a function, it is used in a C statement by complying with the declaration of the function prototype. If the function is defined to receive any arguments, the same number and type of arguments must be passed.

When a function is defined with arguments, the arguments the parenthesis in front of the function name are called formal arguments. In the above example, the function is defined with int a and int b arguments, they are formal arguments.

When the function is called, the arguments passed to it are the actual arguments. In the above example, x and y are the actual arguments.

A C program is a collection of one or more functions. One of the function must be named as main(), which is the entry point of the execution of the program. From inside main(), any function is called. main() can call a library function such as printf() function, whose prototype is fetched from the header file (stdio.h) or any user−defined function present in the code. In C, the order of definition of the program is not material. In a program, wherever there is main(), it is the entry point irrespective of whether it is the first function or not.

In C, any function can call any other function, any number of times. A function call itself also. Such a self−calling function is called a recursive function.

Function Arguments

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.

Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways in which arguments can be passed to a function −

Sr.No. Call Type & Description
1 Call by value

This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

2 Call by reference

This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.

Advertisements