In this tutorial, we will learn functions in C programming. A function is a block of statements that performs a specific task. Let’s say you are writing a C program and you need to perform a same task in that program more than once. In such case you have two options:
a) Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to perform that task.
Using option (b) is a good practice and a good programmer always uses functions while writing code in C.
Why we need functions in C
Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by function calls.
Types of functions
1) Predefined standard library functions
Standard library functions are also known as built-in functions. Functions such as puts()
, gets()
, printf()
, scanf()
etc are standard library functions. These functions are already defined in header files (files with .h extensions are called header files such as stdio.h
), so we just call them whenever there is a need to use them.
For example, printf()
function is defined in <stdio.h> header file so in order to use the printf()
function, we need to include the <stdio.h> header file in our program using #include <stdio.h>
.
2) User Defined functions
The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function.
Now we will learn how to create user defined functions and how to use them in C Programming
Syntax of a function
return_type function_name (argument list) { Set of statements – Block of code }
return_type: Return type can be of any data type such as int, double, char, void, short etc. Don’t worry you will understand these terms better once you go through the examples below.
function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it’s name.
argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.
Block of code: Set of C statements, which will be executed whenever a call will be made to the function.
Do you find above terms confusing? – Do not worry I’m not gonna end this guide until you learn all of them :)
Lets take an example – Suppose you want to create a function to add two integer variables.
Let’s split the problem so that it would be easy to understand –
Function will add the two numbers so it should have some meaningful name like sum, addition, etc. For example lets take the name addition for this function.
return_type addition(argument list)
This function addition adds two integer variables, which means I need two integer variable as input, lets provide two integer parameters in the function signature. The function signature would be –
return_type addition(int num1, int num2)
The result of the sum of two integers would be integer only. Hence function should return an integer value – I got my return type – It would be integer –
int addition(int num1, int num2);
So you got your function prototype or signature. Now you can implement the logic in C program like this:
How to call a function in C?
Consider the following C program
Example1: Creating a user defined function addition()
#include <stdio.h> int addition(int num1, int num2) { int sum; /* Arguments are used here*/ sum = num1+num2; /* Function return type is integer so we are returning * an integer value, the sum of the passed numbers. */ return sum; } int main() { int var1, var2; printf("Enter number 1: "); scanf("%d",&var1); printf("Enter number 2: "); scanf("%d",&var2); /* Calling the function here, the function return type * is integer so we need an integer variable to hold the * returned value of this function. */ int res = addition(var1, var2); printf ("Output: %d", res); return 0; }
Output:
Enter number 1: 100 Enter number 2: 120 Output: 220
Example2: Creating a void user defined function that doesn’t return anything
#include <stdio.h> /* function return type is void and it doesn't have parameters*/ void introduction() { printf("Hi\n"); printf("My name is Chaitanya\n"); printf("How are you?"); /* There is no return statement inside this function, since its * return type is void */ } int main() { /*calling function*/ introduction(); return 0; }
Output:
Hi My name is Chaitanya How are you?
Few Points to Note regarding functions in C:
1) main()
in C program is also a function.
2) Each C program must have at least one function, which is main().
3) There is no limit on number of functions; A C program can have any number of functions.
4) A function can call itself and it is known as “Recursion“. I have written a separate guide for it.
C Functions Terminologies that you must remember
return type: Data type of returned value. It can be void also, in such case function doesn’t return any value.
Note: for example, if function return type is char, then function should return a value of char type and while calling this function the main() function should have a variable of char data type to store the returned value.
Structure would look like –
char abc(char ch1, char ch2) { char ch3; … … return ch3; } int main() { … char c1 = abc('a', 'x'); … }
More Topics on Functions in C
1) Function – Call by value method – In the call by value method the actual arguments are copied to the formal arguments, hence any operation performed by function on arguments doesn’t affect actual parameters.
2) Function – Call by reference method – Unlike call by value, in this method, address of actual arguments (or parameters) is passed to the formal parameters, which means any operation performed on formal parameters affects the value of actual parameters.
Pooja says
Actually it is easy to understand the difference between the function and recursion . simply it is very very useful.
lokesh says
1) why we need return type in programming, why we need to hold a value in return type
2) what the mean of value in return type(like 0, 1, -1)
sophia says
return 0 means that your program has ended successfully without any error.. if you are typing any lines of code below return0.. the compiler will not take that lines…
me says
return 0 is just written to check whether the function has been run successfully without any eror , similarly function can return 1 also . but if it is returning (-1 ) it means program is not running successfully
akshay jain says
can we use multiple function in one program like addition or subtraction
Patel prince says
Yes
Define them
nimra says
yes we can use more than one functions in one program
Mohsin says
Why not, of course! you can use like addiction subtraction multiplication and division in one program, and its too easy.
Utpal says
Does the variables declared in main function need again to be declared in any user defined functions?
D_Jeady says
Yeah, you can! Because a function is a scope of the variable and once a function block is closed then it’s variable functionality ends within that block
So you can create the some variables in a another function. But don’t declare one variable twice in the same function!
Ignatus Anim says
I wouldn’t have suffered in the college if I had new this site earlier. I’m really impressed.
Rupesh Borse says
Is it necessary to use if condition while solving recursion problems ….i tried doing problems without if statement and it’s not working the other way around ….. Majority of them were having if else or simple if condition
Karma says
Is main() function user-defined function even though we can’t change its name ?
Chaitanya Singh says
main() is not a predefined function. It is a user-defined function with a predefined function prototype (also called function signature). As a user we can write its functionality, but its declaration has certain restrictions thats why we cannot change its name.