User-Defined Functions in C
User-Defined Functions in C
User-defined Functions in C
A function in C is a block of organized, reusable code that is used to perform a single
related action. In any C program, there are one or more functions − classified as
library functions and user-defined functions.
Library functions
User-defined functions
Any C compiler (e.g. GCC compiler, Clang, MSVC compiler, etc.) is distributed with a
number precompiled header files (stdio.h, math.h, etc.), each consisting of one or
more predefined library functions such as printf(), scanf(), pow(), sqrt(), etc. To be
able to use the library function, the corresponding header file must be made
available with the #include directive.
However, if you don’t find a suitable library function to serve your purpose, then you
can define a customized function for the program. Normally, we find a C program
with a main() function. Obviously, the main() function is a user-defined function, as
it contains the instructions provided by the user. It can of course call the other
library or user-defined functions.
To create a user-defined function, you need to know about the following three parts
of a function:
Function declaration
https://www.tutorialspoint.com/cprogramming/c_user_defined_functions.htm 1/6
6/16/24, 12:29 PM User-defined Functions in C
Function definition
Function calling
For a user-defined function, its prototype is present in the current program. The
definition of a function and its prototype declaration should match.
Syntax
If you wish to define a function called add() that performs the addition of two integer
arguments and returns the value as an integer, then the function declaration would
be as follows −
The definition of a function and its prototype declaration should match. The definition
consists of a function header that matches the declaration and a function body.
Syntax
// Function body;
return val;
Example
Using this template, you can write the user-defined function add() as follows −
https://www.tutorialspoint.com/cprogramming/c_user_defined_functions.htm 2/6
6/16/24, 12:29 PM User-defined Functions in C
c = a + b;
return c;
}
In a program, the main() function is always the entry point, irrespective of whether
it is the first function or not. We needn't provide the prototype declaration of the
main() function.
To call a function, you should use a statement that complies with the declaration of
the function prototype. If the function is defined to receive a certain number of
arguments, then the same number and type of arguments must be passed to call
that function.
Example
The following statement calls the add() function that we defined above −
// Function declarations
int add(int, int);
int sub(int, int);
// Function definitions
int add(int a, int b) {
return (a + b);
}
https://www.tutorialspoint.com/cprogramming/c_user_defined_functions.htm 3/6
6/16/24, 12:29 PM User-defined Functions in C
int main() {
// Declaring two integer variables to
// store the numbers
// and resultant variables to store the result
int num1 = 36, num2 = 24;
int res_add, res_sub;
return 0;
}
Addition is : 60
Subtraction is : 12
When the function is called, the arguments passed to it are called the actual
arguments. In the example below, the variables "x" and "y" are the actual
arguments.
https://www.tutorialspoint.com/cprogramming/c_user_defined_functions.htm 4/6
6/16/24, 12:29 PM User-defined Functions in C
A function should return a value to the calling environment. By default, the return
type of a function is int type. However, it can return any data type − primary type,
array, pointer, or a struct as well as a pointer. You can even define a function that
returns a void type.
Example
If either the number or the type of actual and formal arguments or the return type
as in the forward declaration of a function and its definition don’t match, then the
compiler reports an error.
#include <stdio.h>
int main(){
int x = 15, y = 5;
printf("%f", z);
return 0;
}
int c = a/b;
return c;
}
Output
In the code above, the declaration of the divide() function doesn’t match with its
definition, hence the compiler shows the following error −
https://www.tutorialspoint.com/cprogramming/c_user_defined_functions.htm 5/6
6/16/24, 12:29 PM User-defined Functions in C
In C, any function can call any other function, any number of times. A function can
call itself too. Such a self-calling function is called a recursive function.
The following program calls the main() function from inside main() itself −
#include <stdio.h>
int main(){
printf("Hello");
main();
return 0;
}
When executed, the program goes into an infinite loop. In practice, recursion has to
be used so that the program eventually terminates.
https://www.tutorialspoint.com/cprogramming/c_user_defined_functions.htm 6/6