In this tutorial, we will learn about the functions and how to use functions in the C programming language with the help of examples.
A function is a collection of statements that are created to perform some specific task. Every program in C is made up of some function. For example, every C program must have a function called main()
. Apart from this main()
function the user can define additional functions as per the requirement.
A large C program can be divided into separate functions which perform a different task. How you divide up your code among different functions is up to the developer, but logically the division is such that each function performs a specific task.
The function is also known as procedure or subroutine in other programming languages.
Advantage of functions in C
- It helps to reduce code redundancy. If a similar piece of code is being used more than once in a code, then instead of typing more than once, you can call a function in those places.
- Editing the code in the function will change and affect all occurrences instead of writing it more than once and having to make changes in all of them.
- It makes the code more modular and decreases the size of the total code. The code becomes easier to read and reuse the code.
- It provides for abstraction in code. We can also use library functions without knowing the full extent of the working of a function.
Types of functions in C
- Library Functions: These types of functions are already defined in the C header file. You can use these functions without knowing much details about these functions in your program. You need to just call it. For example:
floor(x)
,ceil(x)
,exp(x)
, etc. - User-defined Functions: These types of functions are created by C programmers to make their programs more optimized and less complex. They can create functions as many as they want for their program.
In this tutorial, we will focus mostly on User-defined functions.
Aspects of Functions
There are mainly three aspects of functions in the C language.
- Function Declaration – You need to declare a function globally in order to tell the function prototype such as function name, parameters, and the return value.
- Function Definition – This is the actual part where you need to write statements about the functionality of the function. These statements will execute once the control of the program comes into the function.
- Function Call – In order to execute a function, we need to call it from somewhere. One can call the function from any part of the program. We need to pass the same number of parameters while calling it as we have defined the function. Also, note that a function returns only one value after the execution of the function.
Sr. No. | C Function Aspects | Syntax |
1 | Function declaration | return_type function_name (argument list); |
2 | Function definition | return_type function_name (argument list) {function body;} |
3 | Function call | function_name (argument_list) |
Defining a C function
The syntax for defining a function in the C program is as below.
return_type function_name(arg1_type arg1_name, ...){ //body of the function //all the statements here will execute }
Here,
- return_type – The return types of the function define the types the value a function returns. Some function after performing the desired task does not return any value. In this case, the return type of the function is
void
. - function_name – The name of the function you want to keep.
- arg1_type arg1_name – Optional. If you want to pass any parameter to the function, you need to mention it here along with the type of the parameter.
- body of the function – Here all the statements are written to perform the specific task.
Example
The below source of a function named min()
takes two numbers as arguments and returns the minimum among the two numbers.
//function returing the min between two numbers
int min(int x, int y) // User-defined function
{
if (x < y)
return x;
else
return y;
}
C function declarations
A function declaration tells the compiler about function prototypes such as the name of the function, number of arguments, type of the arguments, and the return types of the function. You can define the actual body of the function separately.
The syntax of the C function declaration is as follows.
return_type function_name( parameter_list );
For example, the declaration of the function min() mentioned above can be as follows.
int min(int x, int y);
The name of the parameters is not important in the function declaration, here only the prototype of the function is required. Hence, the below declaration is also valid for the above function.
int min(int, int);
The function declaration is not mandatory when the function definition and function call are in the same source file. It is required when you define a function in one source file and call the function in another source file. In this case, you need to declare the function at the top of the file calling the function.
Calling a C function
In the function definition, you will define what a function will do. Until and unless you call or invoke that function the function will not do anything.
A function is called by just writing the name of the function and passing the arguments inside it if any. Once the function is called, the control of the program is transferred to the called function. The called function performs the defined task and returns the control back to the main()
function when the return statement is encountered inside the function or closing brace is reached.
Different aspects of function calling
A function may or may not accept any argument. Also, a function may or may not returns a value. Based on these facts, there are four aspects of a function call.
- function without arguments and without return value
- function without arguments and with return value
- function with arguments and without return value
- function with arguments and with return value
Example for Function without argument and return value
Example 1
#include<stdio.h> void printMsg(); void main() { printf("Hello! Welcome to "); printMsg(); } void printMsg() { printf("Tutorialsbook"); }
Output
Example 2
#include<stdio.h> void sub(); void main() { printf("\nProgram to calculate the subtraction of two numbers:"); sub(); } void sub() { int a,b; printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); printf("The subtraction is %d",a-b); }
Output
Enter two numbers: 10 3
The subtraction is 7
Example for Function without argument and with return value
#include<stdio.h> int sub(); void main() { int result; printf("\nProgram to calculate the subtraction of two numbers"); result=sub(); printf("The subtraction is %d",result); } int sub() { int a,b; printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); return a-b; }
Output
Enter two numbers:25 15
The subtraction is 10
Example for Function with argument and without return value
Example 1
#include<stdio.h> void sub(int, int); void main() { int a,b; printf("\nProgram to calculate the subtraction of two numbers"); printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); sub(a,b); } void sub(int a, int b) { printf("The subtraction is %d",a-b); }
Output
Enter two numbers:35 10
The subtraction is 25
Example for Function with argument and with return value
Example 1
#include<stdio.h> int sub(int, int); void main() { int a,b,result; printf("\nProgram to calculate the subtraction of two numbers"); printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); result=sub(a,b); printf("The subtraction is %d",result); } int sub(int a, int b) { return a-b; }
Output
Enter two numbers:50 25
The subtraction is 25