by Dinesh Thakur Category: Function and Pointer

C allows programmers to define their own functions. Such functions are called user defined functions. In fact, the main function that must be present in every C program is a user-defined function. A programmer may define additional functions in the following situations:

1. The required functionality is not available as a library function either in the standard C library or in the additional libraries supplied with the C compiler.

2. Some functionality or code is repeated in a program with little or no modification.

3. An existing function is quite large. It is a good idea to divide such functions, if possible, into smaller ones.

Function Definition

The general format to define a function is as follows:

ret_type func_ name ( param _list )

{

    declarations

    executable_statements

}

where funv_ name is the name of the function being defined, ret_type is the type of value returned by the function (also called function type) and param_list is a comma-separated list of function parameters. For each parameter, we must specify the type of the parameter followed by its name. Thus, the param _list takes the following form:

type1 param1, type2 param2, ... , type_n param_n

where type I, type2, ... are the types of parameters paraml, param2, ... , respectively. Note that the rules for naming these parameters (as well as the function name) are the same as those for a variable name and that the type has to be specified separately for each parameter, even if consecutive formal parameters in param _list are of the same type.

A function may not have any parameters. This is indicated by using the keyword void in place of param _list in the function definition. However, void may also be omitted in such cases.

ret_type is the type of the value returned by a function. A function can return only one value or none at all. If a function does not return a value, it is indicated by writing the keyword void in place of ret_type. Also note that ret_type may be omitted, in which case, the function is assumed to return a value of type int.

The body of a function consists of declarations followed by executable statements enclosed within braces '{' and '}'. The entities declared within the function body, which are usually variables, are local to the function being defined, i. e., they are accessible only within the function body and not outside it. We can declare a local variable (or some other entity) with the same name as that of a function. The formal parameters of a function are treated as local variables declared in the beginning of the function body.

The executable statements in the definition of a function can be one or more valid C statements. When a function is called, these statements are executed until a return statement is encountered or all the executable statements are executed.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), 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