Function Basics
Functions:
- Functions are used to divide a large C program into smaller and less complex pieces.
- A function can be called multiple or several times to provide reusability and modularity to the C program.
- Functions are also called procedures, subroutines, or methods.
- A function is also a piece of code that performs a specific task.
A function is nothing but a group of code put together and given a name, and it can be called anytime without writing the whole code again and again in a program.
I know its syntax is a bit difficult to understand, but don’t worry; after reading this whole information about Functions, you will know each and every term or thing related to Functions.
Advantages of Functions
- The use of functions allows us to avoid re-writing the same logic or code over and over again.
- With the help of functions, we can divide the work among the programmers.
- We can easily debug or find bugs in any program using functions.
- They make code readable and less complex.
Aspects of a Function
-
Declaration
This is where a function is declared to tell the compiler about its existence.
-
Definition
A function is defined to get some task executed. (It means when we define a function, we write the whole code of that function, and this is where the actual implementation of the function is done.)
-
Call
This is where a function is called in order to be used.
Types of Functions
-
Library functions:
Library functions are pre-defined functions in C Language. These are the functions that are included in C header files prior to any other part of the code in order to be used.
E.g.
printf()
,scanf()
, etc. -
User-defined functions
User-defined functions are functions created by the programmer for the reduction of the complexity of a program. Rather, these are functions that the user creates as per the requirements of a program.
E.g. Any function created by the programmer.