Functions in C Programming

Functions

A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting the program into separate function make the program more readable and maintainable.

It is necessary to have a single function 'main' in every C program, along with other functions used/defined by the programmer.

A function definition has two principal components: the function header and body of the function. The function header is the data type of return value followed by function name and (optionally) a set of arguments separated by commas and enclosed in parenthesis. Associated type to which function accepts precedes each argument. In general terms function header statement can be written as

return_type function_name (type1 arg1,type2 arg2,..,typen argn)

where return_type represents the data type of the item that is returned by the function, function_name represents the name of the function, and type1,type2,…,typen represents the data type of the arguments arg1,arg2,..,argn.

Example: Following function returns the sum of two integers.

        int add(int p,int q)
         {
         return p+q; //Body of the function
         }

Here p and q are arguments. The arguments are called formal arguments or formal parameters, because they represent the name of the data item that is transferred into the function from the calling portion of the program. The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred.

A function can be invoked whenever it is needed. It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.
e.g., add(5,10);

The following condition must be satisfied for function call.

  • The number of arguments in the function calls and function declaration must be same.

  • The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement.

For example, the following program makes use of function that determine the sum of two integer quantities.

The line of the function contains the function name, 'add' followed by formal arguments p and q, enclosed in parenthesis. The formal arguments p and q represents the data item that are transferred to the function from the calling portion of the program (i.e., add (a, b)). In addition, the formal arguments p and q are preceded by the data type int. i.e., it only accepts integers. When we execute this program the content of the variables a and b are copied to the formal arguments p and q respectively and the function returns the sum of p and q and is assigned to the variable c in the left side of the calling function.

A function may or may not return a value. A 'return' statement returns some value to the calling function and it may be assigned to the variable in the left side of the calling function. The return value can be a constant, variable, a user defined data structure, a general expression, a pointer to function, or a function call. The return statement also causes the program logic to return to the point from which the function was accessed. In general term the return statement is written as

return expression;

If a function does not return a value, the return type in the function definition and declaration is specified as void. The declaration for a prototype for a function that receive any arguments from the calling function and does not return any value will have the format
void function_name (void);

The above program can be rewrite as

        #include <stdio.h>
        #include <conio.h>
       
        int add(int p,int q); // function prototype
        void display(int p, int q, int r); // function prototype
        int main()
         {
         int a,b,c;
         clrscr();
         printf("Enter two numbers\n");
         scanf("%d%d",&a,&b);
         c=add(a,b);
      
         display(a,b,c);
      
         getch();
         return 0;
         }
       
        int add(int p,int q) //This function returns sum of a and b.
         {
         return(p+q);
         }
        void display(int p, int q, int r). //This function returns nothing
         {
         printf("Sum of %d and %d is %d",p,q,r);
         }
 

Ready to get started?

Ready to embark on your journey into the world of C programming? Our comprehensive course provides the perfect starting point for learners of all levels. With engaging lessons, hands-on exercises, and expert guidance, you'll gain the skills and confidence needed to excel in this fundamental programming language. Let's dive in and unlock the endless possibilities of C programming together!