Unit - 05 - Functions - 01
Unit - 05 - Functions - 01
WHAT IS C FUNCTION?
A large C program is divided into basic building blocks called C function. C function contains set
of instructions enclosed by “{ }” which performs specific operation in a C program. Actually,
Collection of these functions creates a C program.
#include<stdio.h>
/*Function prototypes*/
myfunc();
main()
myfunc();
/*Function Defination*/
myfunc()
Need of functions in C
By using functions, we can avoid rewriting same logic/code again and again in a
program.
We can call C functions any number of times in a program and from any place in a
program.
We can track a large C program easily when it is divided into multiple functions.
Reusability is the main achievement of C functions.
However, Function calling is always a overhead in a C program.
Library Functions: Math functions
C –Library functions
Library functions in C language are inbuilt functions which are grouped together and placed in a
common place called library.
We can make use of these library functions to get the pre-defined output instead of
writing our own code to get those outputs.
These library functions are created by the persons who designed and created C
compilers.
All C standard library functions are declared in many header files which are saved as
file_name.h.
Actually, function declaration, definition for macros are given in all header files.
We are including these header files in our C program using “#include<file_name.h>”
command to make use of the functions those are declared in the header files.
When we include header files in our C program using “#include<filename.h>” command,
all C code of the header files are included in C program. Then, this C program is
compiled by compiler and executed.
C Math Functions
There are various methods in math.h header file. The commonly used functions of math.h
header file are given below.
C Math Functions Example
Let's see a simple example of math functions found in math.h header file
#include<stdio.h>
#include <math.h>
int main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
return 0;
Output:
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
Explain any four library functions under conio.h header file.
getche( )-It reads character from keyboard and echoes to o/p screen