What is Function?
A function is a group of statements that together perform a task. Every C program has at least one function, which is main (), and all the most trivial programs can define additional functions.
The C language is similar to most modern programming languages in that it allows the use of functions, self-contained “modules” of code that take inputs, do a computation, and produce outputs. C functions must be TYPED (the return type and the type of all parameters specified).
Why do we use Function in programming?
Reason for functions is to break down a complex program into smaller parts. Suppose we take example of 4 functions we have, 1. Add 2. Sub 3. Mul 4. Div. We have 2 options. First is we take 4 operations in one programs, which becomes very lengthy, alsoit’s very tough to find out errors. This lengthy program also takes more time to load and compile. Second we divide whole programs into smaller modules. So, one main function then remaining 4 modules are 1.ADD 2. Sub 3.Mul 4. Div. So, these 4 operations are done in these separate modules, main function only takes declaration of variables and their input values.
In this modular system compiler time is less compare to first option. We can easily find out the errors module wise. Time and space efficiency is good in modular programming. This module decreases complexity.
Structure of Functions:
Functions that a programmer writes will generally require a prototype. The mult function is actually defined below main. Because its prototype is above main, the compiler still recognizes it as being declared, and so the compiler will not give an error about mult being undeclared.
There are main 3 section for Function in C programming:
- Declaration or Prototype of Function
- Function Call
- Definition of Function
Data Type Function name (Data Type parameter1 name-optional, Data Type parameter2 name-optional); // 1.Declaration or Prototype of Function
Function name (parameter name/Parameter value) //2. Function call
Data Type Function name (DataType formal parameter1, DataType formal parameter2); // 3. Definition of Function
Operation takes place here….
Return statement ();
Here, we can use more than one module in one programme. Compilation start from main always, memory allocated as per variable declared, then function calls statement execute, control transform from main function to called function, there required operation occurs, last return statement execute, then control jump from that module to main module, and execution completed there.
If you like this post then please share and like this post.
Visit us @ tccicomputercoaching.com
Call us@ 98256 18292