FUNCTION IN C

A complex problem is often easier to solve by dividing it into several smaller parts(pieces), each of which can be solved by itself. Each sub-problem is more manageable than the original problem. This is called structured programming. These sub-problems which are easy to manage are made into Functions/Procedures/Modules in C. main() uses functions to solve the original problem. Thus A function is a block of code to perform a specific task. Every C program has at least one function main( ). Without main() function, there is technically no C program.

Advantages of Functions

The following are the advantages of using user defined functions:

1. Functions separate the concept (what is done?) from the implementation (how it is done?).
2. Functions make programs easier to understand thus improves the readability of program.
3. Functions can be called several times in the same program, allowing the code to be reused.
Functions in C
There are 2 types of functions in C programming.

1. Library function
2. User defined function

C allows the use of both User-Defined Functions and Library Functions. Library Functions (e.g printf(), scanf(), getchar(), putchar(), abs(), ceil(), rand(), sqrt(), etc.) are usually grouped into specialized libraries.(e.g. <stdio.h>, <math.h> <string.h> and so on).

FUNCTIONS AND PROGRAM STRUCTURE

C programs usually have the following form include statements, function prototypes/declarations, main() function, function definitions.

Basic structure of C program with function is shown below:
#include <stdio.h> // include statements
void function_name(); //function declaration
int main()
{
...........
...........
function_name(); // function call
...........
...........
}
void function_name() //function definition
{
................
................
}


FUNCTION IN C FUNCTION IN C Reviewed by Unknown on 04:48 Rating: 5

No comments:

Powered by Blogger.