User-defined Functions in C Programming Language
|An important feature of C programming(and almost every other programming language) is that it allows us to create our own functions. In this tutorial, we will be focusing on how to make our own function. There are a few steps that need to be followed in order to create a function:
- Function Declaration: Like variable declaration, a function declaration specifies the type of the function.
- Function Definition: A function needs to be defined in order to be used. It is the actual function that we are going to work on.
- Function parameters: They contain values or arguments that are sent by the calling function.
- Function call: It is how we are going to call the function to get executed.
The function that calls other functions in its body(usually main() function) are known as calling functions and the functions that get called are known as the called functions. This is a two-way communication process that involves sending arguments to the called function and then returning values from the called function to the calling function. Even if we do not have to return anything then we void as the return type. The figure below illustrates the control flow for functions:
Function Declaration
Defining the type of the function is important for creating a function. A function declaration specifies
- the type of the function or return type,
- the function name and
- its parameters.
It is usually done at the top of the program and indicates how the function shall be called. The actual function definition can be present anywhere in the program. Here is how it looks:
return_type function_name(parameters);
- Function return type: It indicates what type of value the function will return. Eg: int, double, float, char, etc. A function with void return type does not return any value.
- Function name: A function name can be anything the user decides. Usually, a function name is kept according to the function it performs.
- Function Parameter: It contains arguments and may or may not take values from the calling function.
Here’s an example of a function declaration with integer return type and area as function name.
int area(int,int);
Function Definition
The function definition consists of the function body along with the return type, function name and its parameters. Here is how it looks like:
return_type function_name(parameter) { //body of the function }
The body of the function contains a list of statements that gets executed once the function is being called. This is known as the called function and after execution of the called function the control goes back to the calling function.
Function Parameter
The function parameter contains the arguments that are passed by the calling function. They are placed within a pair of parenthesis and can have any number of arguments separated by commas. Every parameter has a specific datatype and it accepts only those values which can be assigned to that datatype. A function may or may not have a parameter. A function with no parameter is defined as follows:
void sum() { //body of the function }
In C Programming language, there are two types of function parameters:
- Formal Parameter: The parameter which is written at the function definition is known as a formal parameter.
- Actual Parameter: The parameter that is written at the function call is known as the actual parameter.
The formal and actual parameters are shown below:
int sum(int,int); void main() { int a ,b ; //body of main sum(a ,b );//actual parameter } int sum(int x, int y)//formal parameter { //body of the function }
Function Call
A function needs to be called by the calling function in order to get executed. There are two ways in which a function can be called:
- Call by reference: In this method, the address of the parameter or argument is put in the formal parameter and then the values are accessed using the address in the actual parameter. Call by reference is done as follows:
int sum(int *x,*y)// *x is the address which points to the value of the variable x stored in the address.
- Call by value: In this method, the actual values are passed as an argument in the formal parameter and are accessed in the actual parameter.
int sum(int x,int y) // x is the variable which contains the value so the value is directly passed to the function
Note:- The difference between the above two is that, when we pass by reference then no new copy of the value is created i.e. the passed variable is used and even if we do not return anything from the called function then also the changes will reflect inside the calling function.
Program on Function
Here’s a simple program which prints the factorial of n numbers starting form 1 using functions:
#include <stdio.h> long fact(int); // function declaration int main() { int i, n; long f; printf("Enter the range:\n"); scanf("%d", &n); // each number is sent to the function fact() and it returns the factorial of // the number for (i = 1; i <= n; i++) { // we are using call by value f = fact(i); // actual parameter printf("the factorial of %d is: %d\n", i, f); // prints the factorial of each number } return 0; } long fact(int x) // function definition with formal parameter { int i; long f = 1; for (i = 1; i <= x; i++) { f = f * i; // calculates factorial of the number } return f; }
Output:-
Enter the range:
5
the factorial of 1 is: 1
the factorial of 2 is: 2
the factorial of 3 is: 6
the factorial of 4 is: 24
the factorial of 5 is: 120
Learning never exhausts the mind. So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback.
Do Share and Subscribe.
Keep Coding!! Happy Coding!! 🙂