C Functions is a block of code that takes information in (symbolic names called parameters), do some computation or operations, and (usually) returns a new piece of information based on the input parameter information.
A function is a group of statements that perform together on a specific task. Every C program has at least one function, which is main() (Entry point of every program).
Why to use function ?
Function are used for divide a large code or we can say so many number of lines into program, due to this we can easily debug and maintain the code.
For example if we write a calculator programs at that time we can write every logic in a separate function (For addition–>sum(), for subtraction–>sub()). Any function can be called many times.
Advantage of Function
- Code optimization: No need to write lot of code.
- Easily to debug the program.
- Code Re-usability
Types of Function
There are two types of C functions.
- Library function or pre-define function or inbuilt function.
- User defined function. (UDF)
Library functions are those which are predefined in C compiler. The implementation part of pre-defined functions is available in library files that are .lib/.obj files. .lib or .obj files are contained pre-compiled code. like printf(), scanf(), clrscr(), pow() etc. are pre-defined or inbuilt functions.
User defined function
User define functions are created by programmer according to their needs or requirement for example suppose you want to create a function for subtract two number then you create a function with name subtract () this type of function is called user defined function.
How to define a function ?
- Return type: A function may return a value or nothing. The return_type is the data type of the value the function returns. Return type parameters and returns statement are optional.
- Function name: Function name is the name of function it is decided by programmer or you and it is compulsory.
- Parameters: This is a value which is pass in a function at the time of calling of function A parameter is like a placeholder. It is optional.
- Function body: Function body is the collection of statements or actual operation or logic.
Declaring a Function
A function declaration is the process that tells the compiler about a function name. The actual body of the function can be defined separately.
Note: Declaration time function must be terminated with ;.
Calling a function
When we call any function at that time control goes to function body and execute entire code or statements. For call any function just write function name and if any parameter is required then pass parameter to function.
Example of Function
void Mul(); // declaring a function
int a=10,b=20, c;
void Mul() // defining function
printf("Multiplication: %d", c);
Mul(); // calling function
For More String Function In C