In this tutorial, we will learn abt the user-defined function in C programming language. Let us start by understanding what is user-defined function in C.
A function refers to a block of code that performs a specific task.
User-defined functions are the ones that are defined by the users according to their needs and they perform the task assigned by the users.
Let us say that you want to calculate the area of a circle and also find the circumference of that circle, you can do so by creating two different functions and pass the radius value to those functions. The two functions could be:
- circleArea()
- cricleCircumference()
Example of a simple C user-defined function. The program has a separate function to perform the addition of two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h> // function prototype int addFunction(int x, int y); int main() { int num1, num2, result; printf("Enters the first number: "); scanf("%d", &num1); printf("Enters the second number: "); scanf("%d", &num2); result = addFunction(num1, num2); // function call printf("Result: %d", result); return 0; } int addFunction(int x, int y) // function definition { int sum; sum = x + y; return sum; // return statement } |
Output:
Enters the first number: 5
Enters the second number: 8
Result: 13
Considering the above example, let us go through the following topics:
- function prototype/declaration
- function definition
- function call
- return statement
Function Prototype
Declaration of user-defined function in C.
A function prototype in C is simply a declaration of a function before the main function that does not contain the body of the function. It only specifies the name of the function, its parameter, and the return type of the function.
Syntax of function prototype:
return_type function_name(data_type argument1, data_type argument2, ...);
This line in a program alerts the compiler that the function may be used later in the program.
In the above program, int addFunction(int x, int y);
is the function prototype.
Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> //user-defined function int sum(int a, int b = 5) { ........... ........... return result; } //Main function int main() { ............. ............. // calling a function result = sum(a, b); ............ ............. return 0; } |
Function Definition
Defining a function in C: It is the expansion of function declaration or prototype. It contains the codes within the curly braces {} that perform some specific task.
How to define functions in c:
1 2 3 | return_type function_name( parameter_list ) { body of the function; } |
Explanation of the above syntax:
- Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
- Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
- Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
- Function Body: The function body is enclosed within the curly braces'{}’. It defines the task of a method within the curly braces.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* function that returns the minimum value between two number passed*/ int min(int num1, int num2) { int finalValue; if (num1 > num2) finalValue = num1; else finalValue = num2; return finalValue; //return_type is int } |
Function Call
We call the function by its name and pass the argument(if any) where we want to execute that function. The syntax for calling a function:
function_name(argument_list);
In the above program, result = addFunction(num1, num2);
is the function call.
Passing arguments to a function
In C programming, most of the time we create a function or declare a function that takes and arguments. We call the function by passing a value or more than one value that is referred to as an argument.
These arguments are the variable that is passed while calling a function.
In the example above, result = addFunction(num1, num2);
is the function call and num1
and num2
are the argument that are passed.
Return statement
The return statement in the function is used to terminate the function and return the value to the program line where it is called. The execution control is transferred to the calling function in the program.
Syntax:
return (expression);
In the above program, return sum;
is the return statement of the user defined function.
However, a function can also be called without passing an argument.
Go through the C program below to practice user defined functions programs in C and also learn how to pass an argument in a function.
- C Program to Find Factorial of a Number using Recursion
- Factorial Program in C using Function
- C Program to Reverse a given Number using Recursive Function