Notes, Exercises, Videos, Tests and Things to Remember on Functions
Please scroll down to get to the study materials.
A number of statements grouped into a single logical unit are called as function. It is true that C program is the collection of functions. The function main() in the program is executed first. The other functions are executed when the function main() calls them directly or indirectly.
It is necessary to have a single function main () in every C program along with other functions. It is observed that the main() function is also user defined function. It is executable with suitable parameters.
We are aware of syntax and use of commands, statements, and function in programming languages. C supports the use of library functions which are used to carry out a number of commonly used operations or calculations.
A C program can be modularized through the use of intelligence of such functions. Hence, it is a modular programming. The C language has two types of functions. They are:
1. Built-function or C Library Functions:
The library functions are made available to the programmer in the inclusion of the software product. They are precisely constructed to determine the type of data they accept and the return type.
The library functions such as pow (), sqrt(), strcmp(), etc, are located in the Lib folder by default.
2. User-defined Functions:
These are the functions determined by the users. They are created for calculating different tasks. The general features and facilities obtained from the use of user-defined functions are:
Let us examine a function defined by the user.
int cube (int i)
Retval = i* i * I;
This function is capable of returning a value (integer) since it is explicitly declared in the definition line. The word cube is the function name. The body of the function computes the cube of the value given to i and returns the integer type of value through the variable reveal.
# Write a program to find the cube of 10 during function call and cube of different integer value.
int cube (int i)
retval = i*i*I;
void main ()
/* Cube of constant integer: */
/* function call with fixed value 10 as integer */
printf(“\n The cube of 10 is %d\n”, cube (10));
/*Cube of input integer: */
Printf(“\n Input an inter:”);
printf(“\n The cube of given value %d is %d \n”, inputval, cube (inputval));
You can predict the output. The program will give the cube value of 10 and cube of a given number.
The functions are used to enhance the program. The user defined functions help to calculate the values and return them. The functions can be used in different programs as required. The basic three fundamental properties of function are:
While writing a program, it is split into smaller programs or functions in C. A main program is split into different programs or functions as shown below.
The major activities of the function while programming is:
The function is generally written above the main() function. It is true that main is the main function without other functions cannot execute. It is the entry point. A complete function can be written before the main () function and that can be called from within the main() function.
The generalized concept about the declaration of function could be as follows:
Void sum(); /*function declaration without parameter or arguments*/
Type specifier, function name (arguments);
Type specifier refers to the type of the function such as void, int, float, etc.
Function is the name of the function which can have as many as 40 characters arguments could be int, float, int pointer, etc. which indicates the type of return value in the function.
The function is generally declared before the main(). It may be defined anywhere in the program but it should be declared at the program.
This is the part which executes the function. The function is called from within the main().
The function and its parameters are explicitly included within the function. Let us examine the function call as stated in above program. The last section of this line cube (inputval) is the function call. Although, it could be called in various ways, one of the simplest ways of calling function is shown at this level.
A function can be accessed or called by specifying its name followed by a list of arguments enclosed in the parentheses and separated by commas. If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function.
The arguments appearing in the function call are referred to as actual arguments. In the normal function call, there will be one actual argument for each formal argument. The programmer needs to mention at least the following points about calling a function.
The actual arguments must correspond to the formal arguments in the function definition, i.e., the number of actual arguments must be the same as the number of formal arguments and each argument must be of the same data type as its corresponding formal argument.
Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 194-197.
Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd