In this tutorial you will learn about C Programming - Functions (Part-I) Functions are used in c for the following reasons, Function definition, Types of functions, Functions with no arguments and no return values, Functions with arguments but no return values, Functions with arguments and return values, Return value data type of function and Void functions.
A D V E R T I S E M E N T
A function is a complete and independent program which is used (or invoked) by the main program or other subprograms. A subprogram receives values called arguments from a calling program, performs calculations and returns the results to the calling program.
There are many advantages in using functions in a program they are:
1. It facilitates top down modular programming. In this programming style, the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later.
2. the length of the source program can be reduced by using functions at appropriate places. This factor is critical with microcomputers where memory space is limited.
3. It is easy to locate and isolate a faulty function for further investigation.
4. A function may be used by many other programs this means that a c programmer can build on what others have already done, instead of starting over from scratch.
5. A program can be used to avoid rewriting the same sequence of code at two or more locations in a program. This is especially useful if the code involved is long or complicated.
6. Programming teams does a large percentage of programming. If the program is divided into subprograms, each subprogram can be written by one or two team members of the team rather than having the whole team to work on the complex program
We already know that C support the use of library functions and use defined functions. The library functions are used to carry out a number of commonly used operations or calculations. The user-defined functions are written by the programmer to carry out various individual tasks.
Function definition:
[ data type] function name (argument list)
argument declaration;
{
local variable declarations;
statements;
[return expression]
}
Example :
mul(a,b)
int a,b;
{
int y;
y=a+b;
return y;
}
Scope of Function Variables:
Only a limited amount of information is available within each function. Variables declared within the calling function can't be accessed unless they are passed to the called function as arguments. The only other contact a function might have with the outside world is through global variables.
Local variables are declared within a function. They are created a new each time the function is called, and destroyed on return from the function. Values passed to the function as arguments can also be treated like local variables.
Static variables are slightly different, they don't die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.
Global variables don't die on return from a function. Their value is retained, and is available to any other function which accesses them.
Types of functions:
A function may belong to any one of the following categories:
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with arguments and return values.
Void functions:
The functions that do not return any values can be explicitly defined as void. This prevents any accidental use of these functions in expressions.
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
Functions in C,
functions in c language,
time functions in c,
math functions in c,
substring in c,
function pointers in c,
static function in c,
function pointer in c,
function in c programming,
mod function in c,
rand function in c,
function in c,
static functions in c,
time function in c,
random function in c,
power function in c,
substr in c,
system function in c,