Functions in C

A function is a self contained program segment that carries out some specific. well defined specific task. Every c program contains one or more functions.One of these functions must be called ‘main’.

Advantages of functions:

Once a function is written, it can be called and used repeatedly as and when required. In structured programming, functions serve as the representation for the subtasks that make up the main task. This type of programming is very useful in developing large and complex software. Designing a program will become easy because of the functions. Functions make the debugging of a program easy. Functions will reduce the length of the source code. Functions will help in top down programming approach.

Parts of function:

Any function contains 2 parts.They are:

  • Function declaration.
  • Function definition.

Function declaration:

This is also called ‘function prototype’. It represents function name, function parameters and return type of the function in the following sequence.

 return-type functionname(type parameter1, type parameter2,..);

A parameter is a variable that is useful to receive data from outside into the function. A function may have parameters or may not have any parameters. The return-type represents which type of output or result is returned by the function.

 Example: double sum(double x, double y);

Here, the return-type is ‘double’. Function name is ‘sum’. There are two parameters x and y which are declared as ‘double’ type. so this function receives two double type values from outside and finds the sum and returns the sum value as double type result.

 Example: void sum(double x, double y);

Here, return-type is declared as void. This means this function cannot return any value.

 Example: int square(int num);

This function is returning integer type result. It takes another integer value as input as represented by the parameter num. This function may calculate the square value of a given integer number and return that result.

Example: long int factorial(int n);

This function returns a ‘long int’ type result, which is the factorial value of the given integer number ‘n’.

 Example: void myMenu(void);

‘myMenu’ is the function name. This function does not return any value and does not accept any value.

Function definition

It consists of a function header and body. Here, the function header looks like a function prototype, but there will not be a semicolon at the end. The function body represents a group of statements written inside left and right curly braces. The function body represents the actual logic to solve the task.

Example: int square(int num)       /* this is function header */
           {
              int result;
              result=num*num;
              return result;
           }

Note:

A function can return only one value. It can never return more than one value.

Different ways of writing function:

1. A function without parameters and without a return type.

void sum()
{
    int x=10, y=25;
    int z=x+y;
    printf("\n Result=%d",z);
}

In the above example, sum() does not have any parameters, it cannot accept any values from outside. So, the values are taken inside the function as x and y. They are added, and the result is displayed. Displaying the result does not mean returning the result. It means this function is not returning any results. So, ‘void’ is written before the function. While calling this function, we need not pass any values to it. We can simply call it:

sum();

Program: A function without parameters and without return type.

#include<stdio.h>
void main()
{
  int x=10,y=25;
  int z=x+y;
     printf("\n Result=%d",z);
     }

     void main()
     {    
          sum();
     }
}
Output:
Result=35

2. A function with return type and without parameters.

int sum()
{
    int x=10, y=25;
    int z=x+y;
    return z;   /* return the result */
}

In the above example, observe ‘int’ before the function name. It means this function returns an integer-type result. The following statement accomplishes this:

return z;

While calling this function, we need not pass any values. But we can catch the result returned by this function into an integer type variable, say ‘result’ as:

result=sum();

3. A function with return type but with parameters.

void int( int x, int y)   /* x and y are parameters */
{
    int z=x+y;
    printf("\nResult= %d",z);   /* display the result */
}

In the above example since the function has two parameters, it can accept two integer values from outside. Those values are stores in x and y respectively and then added. While calling this function, we can write:

sum(10,25);

4. A function with return type and with parameters.

int sum( int x, int y)   /* x and y are parameters */
{
    int z=x+y;
    return z;   /* display the result */
}

While calling this type of function, we should pass two integer values to the function, and it will return another integer type result

 result=sum(10,25);  /* pass 10 and 25 to function and store sum into the result */

Calling function:

Once a function is written, it can be called from any program. Also, a function can be called from another function.

square(16);
 or 
square(n);

Here, we are calling the square function and also passing the value 16 is called argument. square() function receives this argument into its parameter and returns square value of 16. This result is 4 and is returned to the calling program or function.

Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.