Functions in C Language

Functions in C Language


A function is a group of statements which are collectively stated under one entity or one name, i.e. function name. A function contains many statements that need to be operated on. It can perform any operation like addition, subtraction or print certain statements or perform any logical operation etc.
C program does not execute the functions directly. It is required to invoke or call that functions. When a function is called in a program then program control goes to the function body. Then, it executes the statements which are involved in a function body. Therefore, it is possible to call function whenever we want to process that functions statements.

Functions Provides us Following Features:

Reusability of Code: Means once a code has developed then we can use that code any time.
Remove Redundancy: Means a user doesn’t need to write code again and again.
Decrease Complexity: Means a Large program will be Stored in the two or more functions. So that this will makes easy for a user to understand that code.

Need of functions in C Language:

As we all know C is procedure oriented programming language and procedure or functions is like the building block of a C program. The entire C program is built with the help of many functions so that it becomes easy for everyone to understand it. The complexity or the difficulty of the program is even decreased since the program is divided into many modules or functions. The detection of errors is even simpler as we can track the error easily.

Advantages of functions:

•    It is easy to use.
•    Debugging is more suitable for programs.
•    It reduces the size of a program.
•    It is easy to understand the actual logic of a program.
•    Highly suited in case of large programs.
•    By using functions in a program, it is possible to construct modular and structured programs.

Type of functions:

Functions are of two types

•    Built in function or Library Functions

•    User defined functions

Built in functions:

Built in functions are the functions that are provided by C library. Many activities in C are carried out using library functions. These functions perform file access, mathematical computations, graphics, memory management etc. A library function is accessed simply by writing the function name, followed by an optional list of arguments and header file of used function should be included with the program.
Definition of built in functions are defined in a special header file. A header file can contain definition of more than one library function but the same function cannot be defined in two header files. These functions are stored in library files. Ex:

•    scanf()
•    printf()
•    strcpy
•    strlwr
•    strcmp
•    strlen
•    strcat

stdio.h: I/O functions:

getchar () : returns the next character typed on the keyboard.
putchar () :outputs a single character to the screen.
printf () :to do input.
pcanf () :for output.
ferror(): Test for File Error
perror(): Print Error Message
vfprintf (): Formatted File Write Using Variable Argument List
vprintf (): Formatted Write Using Variable Argument List
vsprintf (): Formatted String Write Using Variable Argument List

string.h: String functions

strcat (): concatenates a copy of str2 to str1.
strcmp (): compares two strings.
strcpy (): copy contents of str2 to str1.
memset (): Initialize Memory Block
strerror    (): Convert Error Number to String
strlen (): String Length

ctype.h: Character functions

isdigit (): returns non-0 if arg is digit 0 to 9
isalpha (): returns non-0 if arg is a letter of the alphabet
isalnum (): returns non-0 if arg is a letter or digit
islower (): returns non-0 if arg is lowercase letter
isupper (): returns non-0 if arg is uppercase letter

math.h: Mathematics functions

acos () returns arc cosine of arg
asin () returns arc sine of arg
atan () returns arc tangent of arg
cos () returns cosine of arg
exp () returns natural logarithm e
fabs () returns absolute value of num
sqrt () returns square root of num

time.h: Time and Date functions

time () returns current calendar time of system
difftime () returns difference in secs between two times
clock () returns number of system clock cycles since program execution

stdlib.h: Miscellaneous functions

malloc () provides dynamic memory allocation, covered in future sections
srand () used to set the starting point for rand()
exit (): Exit from Program
atof (): Convert String to Floating-Point
atoi (): Convert String to Integer


Post a Comment