C Programming Functions
Contents
What is a function in C?
Functions come into play when there arises a situation where the same codes are executed repeatedly. It is a better practice to build Functions when the same code is used in the different location of the program. The idea is to put together all the common types of codes together as Functions.
A function is a named independent section of code that performs a specific task. Optionally, It returns a value to a calling program. Without the use of the function, the program will be buggy and it will be hard to manage. Writing the same block of code, again and again, will be boring for the programmers. We can use the function at any part of our program simply by calling them.
C programming provides two different categories of functions. You can see the description of the categories in detail below.
Predefined functions:
Those functions that are available in the ‘C’ standard library are called Predefined functions. They are the functions whose definitions will be on the header files such as stdio.h, string.h etc. Those functions are already defined to the compiler of the C language. The functions under this header files are printf (), scanf(), puts, gets etc. All you need to do is to include an appropriate header file to use these functions. Those functions are declared and defined in the C Libraries.
User-defined functions:
C Language provides us with a privilege to create the function of our own. We can define these functions at the time of writing program. Functions that the programmer creates for a specific task are known as a user-defined function. There are 4 types of user-defined functions. These types of functions are defined for the reusability of the codes and to save time and space. We can define the function of our own in 4 different ways. In general, programmers can go for any of these ways will writing their programs.
- No return type and no argument
- No return type with the argument
- With return type and no argument.
- Return type with the argument.
Click here to learn in details about the types of functions.
How to use the function in our program?
We just need to perform 3 different tasks while using functions in C. We will see the tasks that we perform while using the function in detail below.
Function definition:
The function definition contains two principal components. The first component is the name and argument declaration and the second component is the body of the function. Look at the syntax of function definition below.
Syntax:
return type function name ([data type argument 1, data type argument 2,….])
{
statement 1
statement 2;
statement 3;
………………..
………………..
}
Function prototype
Whenever you define a function, you just need to declare a prototype above the main function. This is preferable in the top-down approach of programming in which the main appears before the user-defined function definition. In the bottom-up programming approach, where we define functions above the main we do not need to use function prototypes. In top-down programming approach, By defining a function prototype the compiler is first alerted to the fact that the function being accessed will be defined later in the program.
Syntax:
{
statement 1;
……………….
}
Function call:
We can simply call the functions by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.
Syntax:
What are the Actual and formal parameters?
The parameters in the function call consist of the list of arguments that the function expects. We define these arguments within the function. The arguments defined within the called function are called formal parameters or formal arguments. We will actually use formal parameters in the function definitions. On the other hand, the actual arguments or actual parameters are those arguments which we will use to call the function from the calling function. We will use these parameters in the function call.
Why is Function important in C?
- The function will enforce modularity in our program.
- Function helps in code reusability and makes debugging easier. We just simply need to call the function by its name whenever required.
- With the help of function, it becomes easier to distribute different tasks to the programmers by specifying different functions.
- The program will consume less space and time with the help of functions.
- Functions make the programs readable and easy to understand.
- In the case of large programs, functions will manage the codes
Example of Function in C:
Now let’s move on to our first code implementing the function in C. The below program is a simple program that will add any two given numbers. Firstly, the program will ask the user to input numbers. We will then define a function that will compute the result of the addition. If our function returns value then we will have to specify the return type either int or float or any datatype depending on the value the function will return. If or function won’t return any value then we have to specify its return type as a void. The below example deploys all the parts of functions that will help to clarify the concept of it more.
Code:
#include<stdio.h> int Add(int, int); int main() { int a,b, sum; printf("Enter any two numbers: "); scanf("%d%d", &a,&b); sum=Add(a,b); printf("The sum of any two given numbers is: %d",sum); } int Add(int x, int y){ int sum; sum = x+y; return sum; }
The above program presents the top-down approach to programming. So we have to specify the prototype of the function before the main function. Here the function is using the return type ‘ int’ which indicates that the function will return an integer value to the main program. Moreover, we can also use void instead of the int in the above program. But in that case, the function won’t return anything. So we will have to print the result within the function definition. Thus, we pass two numbers to our function ‘sum’ which will compute the sum by adding two numbers and returns the final result to its respective calling function.
Output
3
4
The sum of any two given numbers is: 7