Home » Programming Languages » C Programs » Functions in C

Functions in C

Function in C programming is a group of statements which are separated from main task for the purpose of calling it repeatedly, as and when required. A function normally does a predefined task and returns the result of operations to the statement from where this function is called.

Functions greatly help to simplify the main program / source code you write and makes it easier to understand. How ? we will understand as below,

Lets consider the following main program which does the addition of two numbers few times as,

int main(int argc, char **argv) {
    int i = 20, j = 32, add;
    add = i + j;
    printf("addition = %d\n", add);
    add = 35 + 45;
    printf("addition = %d\n", add);
    add = 75 + 19;
    printf("addition = %d\n", add);
    return 0;
}

As you can see above, we added two numbers few times and every time printed the output of additions of this two numbers. Above code is very simple, but what if every operation (like addition here), is complicated in our main program, then the main program grows bigger and after some time the developer starts getting confused where to add what..

So the solution to this problem is use a function. Function is a group of statements which we do repeatedly but used for single purpose. ( For example: addition as here )

Now, lets define the declaration of the function. Functions are declared as,

return_type function_name(input variable list);

As seen above, a function can have any name (except not used by libraries which are linked with this program’s final executable. For example: we can’t use our functions name as “malloc” otherwise compiler will show errors. As malloc is a library function and name of the function we want to write should be custom user defined)

So, function accepts the list of input variable ( it can be nothing i.e. void or as much as we want each separated by comma) and returns the result as required using data type “return_type”. Function return type also can be void. i.e. function doesn’t return anything.

Now, lets see the definition of function.

return_type function_name(input variable list) {
    ... function body ...
}

Above is how the functions are written, and below if the simple definition of addition function.

int function_add(int k, int l) {
	return (k+l);
}

Above, is the simpler definition of function, which accepts two integers k & l, does the addition and returns the result as integer (check return type as “int”)

Example of using “Functions” in C

Now, we will show the example of writing simple function which name is “function_add” . It adds two intergers and return result as interger. Then main function calls this function inside its body and prints the results returned.

#include <stdio.h>

// Function declaration
int function_add(int, int);

int main(int argc, char **argv) {
	int i = 10, j = 32, add;

	add = function_add(i,j);
	printf("addition = %d\n", add);

	return 0;
}

int function_add(int k, int l) {
	return (k+l);
}

Disadvantage :

The only disadvantage of the function (normally for embedded hardware’s / cpu’s) is there is some time taken by processor to call and execute this function. But in today’s world where there are high competitive CPU are available, the time taken to call the function is negligible.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment