A function is a set of statements that take inputs, perform certain calculations, and produce results.
C functions are the basic components of a program. All C programs are written with features to improve reuse, understanding, and tracking. In this section, you can learn more about the following C function concepts.
The general form of a function is:
return_type function_name([ arg1_type arg1_name, … ]) { code }
Uses of Function in C Language :
- C functions are used to prevent the same logic / code from being rewritten multiple times in a program.
- There are no restrictions on calling C functions to use the same functionality as needed.
- You can call a function as many times as you like in your program from anywhere in your program.
- Large C programs can be easily traced by dividing them into functions.
- The central concept of C functions is reuse. Break large tasks into smaller chunks to achieve functionality and better understanding of very large C programs.
For example:
Here Code:
#include <stdio.h>
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main(void)
{
int a = 10, b = 20;
int m = max(a, b);
printf("m is %d", m);
return 0;
}
Why do we need functions in C Language?
- Features help reduce code redundancy. When a function runs in multiple places in the software, instead of writing the same code, create the function over and over again and call it everywhere. This is also useful for maintenance, as future feature changes will need to be changed in one place.
- Functions modularize your code. Consider a large file with many lines of code. Dividing the code into functions makes it much easier to read and use the code.
- Functions provide an abstraction. For example, library functions can be used without concern for internal behavior.
Types of functions in C Language:
1) Predefined standard library functions:
Functions like put (), gets (), printf (), and scanf () are standard library functions. These functions are already defined in header files (files with an .h extension are called header files like stdio.h), so call them whenever you need to use them.
For example, the printf () function is defined in the header file <stdio.h>, so to use the printf () function, use #include <stdio.h> to program the header file <stdio.h>.
2) User Defined functions:
The functions that you create programmatically are called user-defined functions. That is, user-created functions are called user-defined functions.
Next, you 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
}
Where;
return_type: return type can be any data type like int, double, char, void, short. You can better understand these terms by running the examples below.
function_name: It can be any name, but it is a good idea to give the function a meaningful name so that you can easily understand the purpose of the function.
Argument list: The argument list contains variable names and their data types. These arguments are an input type for a function. Example: the function used to add two integer variables has two integer arguments.
Code block: A set of C statements that are executed each time a function is called.
Function Declaration:
The function declaration tells the compiler how many parameters the function takes, the data type of the parameters, and the return type of the function. Putting the parameter name in the function declaration is optional in the function declaration but must be in the definition.
It’s always a good idea to declare the function before using it (see this, this, and this for more information).
In C, you can run declarations and definitions in the same place, as in the sample program above.
In C, you can also declare and define functions individually. This is especially necessary for library functions. Library functions are declared in the header file and defined in the library file. The following is an example statement.
Passing parameters to the function:
The parameters passed to the function are called actual parameters. For example, in the above program, 10 and 20 are the actual parameters.
There are two most common ways to pass parameters.
Pass by values: This method of passing parameters copies the actual values of the parameters to the formal parameters of the function and stores the two parameters in different memory locations.
Passing by reference: Since both the actual and formal parameters refer to the same location, changes made within the function are reflected in the actual parameters of the caller.
Parameters are always passed as C values. For example. In the following code, the value of x has not changed using the fun () function.
Here Code:
#include <stdio.h>
void fun(int x)
{
x = 30;
}
int main(void)
{
int x = 20;
fun(x);
printf("x = %d", x);
return 0;
}
However, in C you can use pointers to get the effect of passing by reference. For example, consider the following program. The fun () function expects a ptr pointer to an integer (or an integer address). Change the value of the ptr address. The dereference operator * is used to access the value of an address. The instruction “* ptr = 30” changes the value of the address ptr to 30. The address operator & is used to obtain the address of a variable of any data type. The function call statement “fun (& x)” passes the address of x, so x can be modified using that address.
Here Code:
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}
int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);
return 0;
}
Benefit of functions in C Language:
C functions have the following advantages:
- By using functions, you can avoid having to rewrite the same logic / code multiple times in your program.
- C functions can be called any number of times in the program from anywhere in the program.
- Divide a large C program into several functions for easy tracking.
- Reuse is a major achievement of the C functions.
- However, function calls are always an overload of the C program.
Conclusion:
As you know, C functions are the building blocks of all C programs. Here are some important points to keep in mind in order to efficiently create your own functions in a C program using existing functions from the C library.
Learn More About Pointers in C Language
References:
- https://www.javatpoint.com/functions-in-c
- https://www.programiz.com/c-programming/c-functions
- https://www.tutorialspoint.com/cprogramming/c_functions.htm
- https://beginnersbook.com/2014/01/c-functions-examples/
- https://www.geeksforgeeks.org/functions-in-c/
- https://www.studytonight.com/c/user-defined-functions-in-c.php