Types of Function
Library Function: C has some library function which is used by the C compiler and C also has the facility to provide some library function to the programmer for doing some operations. Suppose that if we want to find out the length of string then we will use the library function strlen( ) and suppose we want to compare the two string then we will use the library function strcmp() and many more library functions are also there in the C library.
Mathematical functions are called by using the preprocessor directive #include<math.h> and string library functions are called by #include<string.h>.
'C' language is accompanied with several library functions that carry out various commonly used operations or calculations. Every library function has its prototype defined in a header file.
Some Library Function:
abs (): It returns the absolute value of an integer.
Syntax: int abs (int x);
Prototype: math.h
ceil (): Rounds up a number to an integer greater than or equal to given value.
Syntax: double ceil (double x);
Prototype: math.h
floor (): Rounds down a number to an integer less than or equal to given value.
Syntax: double floor (double x);
Prototype: math.h
fmod (): Divides first number to second number and returns modules.
Syntax: int fmod (int x , int y);
Prototype: math.h
log (): Returns the natural logarithm of a number.
Syntax: int log (int x);
Prototype: math.h
log10 (): Returns log10 of a given number.
Syntax: int log10 (int x);
Prototype: math.h
pow ( ): Returns first number raised to the power of second number.
Syntax: int pow (int x , int y);
Prototype: math.h
pow10(): Return 10 raised to the power.
Syntax: int pow10 (int x);
Prototype: math.h
sqrt (): Returns square root of a given number.
Syntax: int fmod (int x);
Prototype: math.h
sound (): This function produces the sound at specified frequency in hertz (cycle per second ).
Syntax: void sound (unsigned frequency);
Prototype: dos.h
nosound (): Turn off the sound.
Syntax: void nosound (void);
Prototype: dos.h
delay (): Suspend execution for an interval of specified millisecond.
Syntax: void delay ( unsigned millisecond );
Prototype: dos.h
abort (): Abnormally terminates a process and writes a termination message "Abnormal program termination".
Syntax: void abort (void);
Prototype: stdlib.h , process.h
exit (): Terminates a program.
Syntax: void exit (int status);
Prototype: stdlib.h , process.h
gotoxy (): Move cursor to the position in the current text window.
Syntax: void gotoxy ( int a , int b );
Prototype: conio.h
textbackground ( ): Select a new text background color.
Syntax: void textbackground (int newcolor);
Prototype: conio.h
textcolor ( ): Select new characters color in text mode.
Syntax: void textbackground (int newcolor);
Prototype: conio.h
atoi (): Converts a string to integer.
Syntax: int atoi (char s);
Prototype: stdlib.h
User defined Function: User can also create own functions for doing some specific task of the program. Such functions are called user-defined functions. Every function must be called in the main ( ). After each function has done its operation, control returns back to the main( ). Then remaining statements of main( ) are executed. Four types of the user defined functions are used in the C language:
(a) Function with no argument and no return value.
(b) Function with argument but no return value.
(c) Function with argument and return value.
(d) Function without argument and but return values
Function with no argument and no return value
Function which have no argument and no return value are written as
main( )
{
................
func( );
.................
}
func( )
{
................
statements
................
}
In the above example the function func( ) is called by the main( ) and the code of the function (body of the function) is written after the main( ) function. As the function func( ) has no arguments, main( ) cannot send any data to func( ) and since it has no return statement, hence function cannot return any value to main ( ).
Note:
1. Neither the data is passed through the calling function not the data is sent back from the called function.
2. The function is only executed and nothing is obtained.
3. There is no data transfer between calling and the called functions.
4. If such functions are used to perform any operation, they act independently. They read data values and print result in the same block.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
printf("I am in main\n");
sikar();
jaipur();
bangalore();
}
sikar()
{
printf("I am in italy\n");
}
jaipur()
{
printf("I am in jaipur\n");
}
bangalore()
{
printf("I am in bangalore\n");
}
Output:
I am in main
I am in sikar
I am in jaipur
I am in bangalore
Function with argument but no return value
Since such functions have parameters, hence the calling function can send data to the called function but it cannot return any valued to the calling functions, as it has no return statement. This function can be written as -
main( )
{
.......................
func(a,b);
}
func(int c,int d)
{
.....................
statements
.....................
}
Here a and b are actual parameters which are used for sending the value. c and d are the formal parameters, which take values from the actual parameters. It is necessary to declare the data type of the formal parameters before defining the function.
Note:
1. In the above function arguments are passed through the calling function. The called function operates on the values. But no result is sent back.
2. Such functions are partly dependent on the calling function. The result obtained is utilized by the called function and there is no gain to the main().
Program: Write a program to swap the two values without using the third variable using the function with argument but no return statement.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int swap(int,int); //prototype declaration
int a,b;
clrscr();
printf("Enter two values:");
scanf("%d%d",&a,&b);
printf("\nBefore interchange the value of a=%d b=%d",a,b);
swap(a,b); //calling function
}
swap(int x, int y) //function definition
{
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter interchange the value of a=%d b=%d",a,b);
}
Function with argument and return value
Since such functions have parameters, the calling function can send data to the called function; it can also return any value to the calling function with the use of return statement. This function can be written as-
main( )
{
..................
func(a,b);
..................
}
func(a,b)
int a,b;
{
..................
..................
return(expression);
}
Here return statement returns the value of the expression to the calling function.
Note:
1. In the above example the copy of actual argument is passed to the formal argument.
2. The return statement returns the value that is written in the return statement.
3. In this example, the data is transferred between calling and called functions i.e. communication between functions is made.
/*Program for sending and receiving values between functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
printf("Enter any three numbers");
scanf("%d%d%d", &a,&b,&c);
sum=calsum(a,b,c); //Here a and b is the actual arguments
printf("\n Sum=%d",sum);
}
calsum(x,y,z) //Here x ,y and z are the formal arguments
int x,y,z;
{
int d;
d=x+y+z;
return(d);
}
Output:
Enter any three numbers 10 20 30
Sum=60
Function without argument but with return value
main()
{
int z;
.......................
z=abc();
.......................
.......................
}
abc()
{
int y=5;
........................
........................
........................
return (y);
}
Note:
1. In the above type of function no argument(s) are passed through the main() function. But the called function returns the values.
2. The called function is independent. It reads values from the keyboard or generates from initialization and returns the values.
3. Here both the calling and the called functions are partly communicated with each other.
/*Program to receive values from user-defined function without passing any value through main().*/
#include<stdio.h>
#include<conio.h>
main()
{
int sum(),a,s;
clrscr();
s=sum();
printf("Sum=%d",s);
}
sum()
{
int x,y,z;
printf("\n Enter three values:");
scanf("%d%d%d",&x,&y,&z);
return(x+y+z);
}
Output:
Enter three values: 3 6 9
Sum=18