Functions in C

Functions:

A function is self contained block of statements enclosed between braces that performs a specific task. It groups a number of repeated statements in a program into a single unit and is identified by name known as function name.A program can contain one or more functions, but the main() function must be part of every program. It is the main() function from where the program execution always begins and ends. Other additional functions must be subordinate to it and to one another. They are directly or indirectly called main() to do special tasks. A programmer can invoke a function at different places in a program whenever it is needed instead of writing the same set of statements again and again in the program. After the function has performed intended task, the control is returned back to the calling function and continues with the rest of the statements in its body.For example: Suppose we want to write a program that calculates the binomial coefficient using the formula,    nCr =n!/r!(n-r!)
For this, we need to calculate the factorial of a number n, r and r-n at the different places in the program. You could write the code for them but you may observe  that code computing factorial for n,r and n-r are very similar except that only the values as different. This obviously involves a lot of repetition. Wouldn't be nice if the common code that calculates the factorial is written only once and reuse it without  rewriting it. This can be achieved by defining a function. By writing the code once as a function, the overall size of the program is reduced, repetition are avoided and designing  the program becomes easier and faster.

Advantages of functions:

The  following are the advantages of using functions in a program.
  • Functions support modular programming in which a large program is divided into smaller self contained parts , each of which has a unique identifiable purpose. This helps in making the program easier to write . understand, debug and maintain.
  • functions result is reducing the overall size of the program as repetitive set of instructions are written only once in the function. Thus instead of writing , debugging and compiling the same set of instructions again and again , the some code can be reused .
  • Functions promote portability since functions written are independent of system dependent features.
  • Designing large and complex programs become every easy and fast using functions. This is because different person can be involved in making different functions which can be later combined to make the complete program. Thus it result in reduction of time and cost.
  • different data set can be transferred to the function each time it is invoked, thus it helps to save a lot of memory space by allowing same set of statements to be used for different sets of data.
  • The use of functions enables the programmer to build a customized library of frequently used functions which can be accessed by different programmers. This result in reducing the cost of writing , debugging, testing and size of a program.
  • In C, a function can be call itself again, this is known as recursion. Using recursion many calculations can be done very easily. For example , calculating factorial of a number using recursion.

Classification of functions:

Function can be classified broadly into two categories  : Library functions(or in-built functions) and user-defined functions.

Library functions:

There are certain set of general purpose operations which are quite frequently used by the programmers in their programs. For example :To calculate the square root of a number, to calculate power of number,to calculate the length of the string , to perform input/output and many more . Making functions for performing these operations in every program is unnecessary and time consuming job. So these general purpose operations are programmed and stored in C library so that they can be called through any program in the form of functions. These functions are known as the library functions.
The library functions (also known as in-built functions) are predefined and precompiled functions that are  designed to perform some specific task. Information is passed to these functions via argument and returned via  the return statement. Some of these functions returns a value, while some functions do not return any value at all.
There are numbers of library functions available in C. Some of the library functions and their corresponding header files are given below:
(a). Library functions that carry out standard input/output operations. Some of these include scanf(),printf(), getchar(),putchar() etc. these these are included in stdio.h header file.
Some another library functions in stdio.h file get(s) its return type is char* , put(s), printf(...), scanf(...), getchar(void), putchar(c) its return types are int.
Beside these, some other library functions that are part of stdio.h library file are fclose(), feof(), fgetc(), fopen(), fprintf(), fputs(), fread(), fscanf(), fseek(), rewind().

(b). Library functions that carry out operations on argument of type character. Some of these include tolower(), toupper() etc. These are collectively under ctype.h header file.
Some another library functions in ctype.h file tolower(c), toupper(c), toascii(c), isalpha(c), isdigit(c), isupper(c), its all are contain int return type .
Beside these, Some other library functions available in this header file are isalum(), iscntrl(), isgraph(), islower(), isodigit (), isprint(), isspace(), isxdigit(). All these functions terurn non-zero value if true, 0 otherwise.

(c). Library functions that perform operations on strings : Some of these include strcmp(), styrcpy, strlen() , strcat(), etc. These are collectively grouped in string.h header file.
here, strlen(s) and strcmp(s1, s2) contain int return type and strcpy(s1, s2) and strcat(s1,  s2) contain char* return type.

(d). Library functions that perform operations to carry out various mathematical calculations. These mathematical functions are part of math.h header file.
Some of these include acos(d),asin(d), atan(d), ceil(d), cos(d), cosh(d), exp(d), fabs(d), log(d), log10(d), pow(d1,d2), sin(d), sqrt(d). These all are contain double return type.

Accessing a library function:

A library function can be accessed simply by writing the function name, followed by the list of arguments that represent the information being passed to the function. The arguments must be enclosed in parentheses and separated by commas, if any. For example, suppose we want to calculate the square of a number 16. For this, we will use the predefined function sqrt() available in math.h header file. We shall provide this function with a number whose square root we want to calculate (16 in our case) and it should provide us with calculated square root of this number as the output. The number that we provide to this function is called its argument and the number that is provided as output is the return value. Some function may have more than one argument but a function cannot return more than one value. A call made to this function is written as,
    sqrt(16 .0);      /*returns 4.0*/
The value returned from the function can be saved for further calculation or simply displayed some examples are:
1. x= sqrt (16.0);
2. area = sqrt((x-a)*(x-b)*(x-c));
3. printf("\nSquare root = %f" , sqrt(16.0));

A program of library function to check whether the entered character is a letter, digit or none of these?
#include(stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
char ch;
clrscr();
printf("enter the character : ");
ch = getchar();
if(isalpha(ch));
printf(" the character is a letter : ");
else if (isdigit(ch)>0)
printf("the character is a digit : ");
else
printf("not digit not letter");
getch();
}

OUTPUT:
enter the character : @
not digit not letter

                     THANKS  


No comments

Powered by Blogger.