Functions in C/C++

What are the functions in C and why should we use them?

Suppose you have to write a code in which you have to find out the sum and difference of two numbers three times. Well, it’s very simple to do that but now you have to write the same code three times. To make things easier for you, there are functions in which you write code and then use that function later inside the code. In this article, we will learn how to declare, define and use functions in C.

Function Syntax in C

The syntax of a function definition is –

Return_type function_name(function args) {
//body of the function
}

 

Function args and Return type-

Function arguments are the value you needed to be passed while calling the function. For example, you will need two integer type number so that you can write the function to add two values. We use arguments inside the body of the function where you write a code about what to do with it.

Return type signifies the type of data returns from the function. It can be int, float or even a void. Needless to say that you don’t need to return anything when there is a void return type. Check out this example –

#include <stdio.h>

int sum(int a, int b){
	printf("Inside sum\n");
	return (a+b);
}

int main(void) {
	int c = sum(10,20);
	printf("The value of sum is %d\n",c);
	return 0;
}

Here I need the sum of two number and therefore I am returning the sum of the two number at the last of the function.

Function Declaration and Function definition

Look at this section of the program. Here I am declaring the function before actually defining it. In C, you generally need to declare your function before actually defining it. The standard functions you use – “printf()” and “scanf()” are already declared in the header file(“.h”) that you have included in the program. In C, if you call the function before defining it, you might receive an error or warning message. Consider the following example-

#include <stdio.h>

int main(void) {
	int c = sum(10,20);
	printf("The value of sum is %d\n",c);
	return 0;
}

int sum(int a, int b){
	printf("Inside sum\n");
	return (a+b);
}

On executing this program, you should receive a warning message about the implicit declaration of function sum. It means the compiler doesn’t understand where the function is at the time of compiling it. But if you don’t want to change the structure of the program, you can declare the function at the starting of the program-

#include <stdio.h>

int sum(int, int);
int main(void) {
	int c = sum(10,20);
	printf("The value of sum is %d\n",c);
	return 0;
}

int sum(int a, int b){
	printf("Inside sum\n");
	return (a+b);
}

Now that you may have already noticed, main() is a function too with the integer return type.

Benefits of function in C

  • Well, the first benefit is very clear, you don’t need to write the same code in multiple places.
  • It increases the modularity of the code. What I mean by modularity here when you write a large piece of code with so many functions, each function should be independent of each other and that is advantageous because changing a function doesn’t change the whole functioning of the program.
  • The code is easily maintainable when you use functions.

There is also one more use of function in C and that is recursion but I don’t think that discussion about it is in the scope of this article. If you find any problem in this article, feel free to write in the comment section below. Check out other articles as well.

Leave a Comment

Your email address will not be published. Required fields are marked *

Share This