Functions In C
FUNCTIONS In c Language
A function is a self-contained block of code that performs a particular task. Once a function has been designed and packed, it can be treated as a ’black box’ that takes some data from the main program and returns a value. The inner details of operation are invisible to the rest of the program. All that program knows about a function is: what goes in and what comes out. Every program can be designed using a collection of these black boxes known as functions.
C functions can be classified into two categories:
(i) Library functions or Built-in functions, and(ii) User-defined functions.
Main is an example of user-defined functions. The distinction between these two categories is that library functions are not written by us whereas a user-defined function has to be developed by the user at the time of writing a program. However, a user defined function can later become a part of the C program library.
Every program in C must have a main function to indicate where the program has to begin its execution. While it is possible to code any program utilizing only main function, it leads to a number of problems. The program may become too large and complex and as a result the task of debugging, testing, and maintaining becomes difficult. If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit. These subprograms called ’functions’ are much easier to understand, debug, and test.
There are times when certain type of operations or calculations is repeated at many points throughout a program. For instance, we might use the perfect square of a number at several points in the program. In such situations, we may repeat the program statements wherever they are needed. Another approach is to design a function that can be called and used whenever required. This saves both time and space.
This division has a number of advantages:
It facilitates the 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 function are addressed later.
Types of Function
- Predefined Standard Library Functions
Such as Put(),gets(), printf(),scanf(),etc. these are already Predefined in Header files, we just call them whenever their requirement.
- User Defined Functions
The Functions that we create.
syntax for Function - return_type Function_name(argument list)
{
Set of Statements
}
Creating User defined functionswe will solve A example where we define Functions to ADD two numbers
Declaration
- sum=num1+num2 (formula for addition)
OUTPUT see other program examples for better understanding |
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment