Functions:
> Function is a collection of different statements which can perform a specific
task in the program.
> We can write a program using functions, is called Modular
Programming.
> There are many advantages
of using functions:
-
Easy
to write and understanding programs
-
Compact
size of the program
-
Easy
to compile and execute
-
Fast
execution of programs
-
Easy
to Debug and Update the program
-
Cost
effective
-
Time
saving
> There are 2 types of functions:
a) Pre-Defined Functions
(PDF)
b) User-Defined
Functions (UDF)
Pre-Defined Functions
|
User-Defined Functions
|
These
functions are already defined in ‘C’ Library.
|
These
functions are defined by the User on demand in
the program.
|
Required
a specific Header File in the program before
using it.
|
Don’t require any specific Header file.
|
Visible
as global, means we can call it inside any of
the program.
(because
of Header files)
|
Visible
as local, means we can call it inside that
program where it is defined.
|
Also
called Library/In-built Functions.
|
|
Ex:
printf( ), scanf( ), clrscr( ), strlen( ), sqrt( ), etc.
|
Ex:
main( ), etc.
|
> We may PASS
one or more value to the function as Argument (parameter) and it may RETURN a value.
~~~~~~~~~~~~@~~~~~~~~~~~~
Pre-Defined Functions:
===================
There
are following types of PDFs:
Type
|
Required Header File
|
Function List
|
Standard I/O Functions
|
stdio.h
|
printf(
), scanf( )
|
Console I/O Functions
|
conio.h
|
getchar(
), getche( ), getch( ), putchar( ), putch( ), gets( ), puts( )
|
String Functions
|
string.h
|
strlen(
), strcpy( ), strcmp( ), strcat( ), strrev( )
|
Mathematical
/ Arithmetic Functions
|
math.h
|
sin(
), cos( ), tan( ), abs( ), sqrt( ), fmod(
), ceil( ), floor( ), pow( ), exp( ), log( ), log10( )
|
Dynamic
Memory Allocation (DMA) Functions
|
alloc.h
|
malloc(
), calloc( ), realloc(
), free( )
|
File
I/O Functions
|
stdio.h
, conio.h
|
fprintf(
), fscanf( ), feof( ), fopen( ), fclose( )
|
~~~~~~~~~~~~@~~~~~~~~~~~~
Arithmetic Functions:
> These functions are used to perform different mathematical (arithmetical) operations
on numbers, like finding Square Root, Absolute value, etc. of given number.
> We will pass one or more value to the function
and it will return only one value.
> Required a header file “math.h”.
> Ex: abs( ), sin( ), sqrt( ), etc.
sqrt( ) - This function is used to find Square Root of
any Positive number.
Syntax:
sqrt(value or variable)
Ex:
sqrt(16) =>
4
abs( ) - It returns absolute value of given number.
(unsigned value)
Syntax:
abs(value or variable)
Ex:
abs(+5) =>
5
abs(-7) =>
7
abs(8) =>
8
fmod(a, b) - It returns remainder
value after dividing ‘a’ by ‘b’. (a%b)
Syntax:
fmod(a , b)
Ex:
7 % 2 =>
1
fmod(7, 2) =>
1
fmod(66, 2) =>
0
pow(x, y) - It returns a value ‘x’ to the raised power
‘y’. => (x)y
Syntax:
pow(x, y)
Ex:
pow(2, 5) =>
32 (2)5 = 2x2x2x2x2 = 32
exp(x) - It returns exponential value of given number
‘x’. (ex )
Syntax:
exp(x)
Ex:
exp(0) =>
1
exp(1) =>
2.718282
exp(2) =>
20.085537
printf(“%f”, exp(2)); => 20.085537
log(n) - It returns Natural Logarithm value of given number ‘n’.
Syntax:
log(n)
Ex:
log(1) =>
0
log(2) =>
0.693147
log(10) =>
2.302585
log10(n) - It returns Logarithm
value to the base 10 of given number ‘n’.
Syntax:
log10(n)
Ex:
log10(1) =>
0
log10(2) =>
0.301030
log10(10) =>
1
ceil(n) - It returns a value rounded
up to the next higher integer.
Syntax:
ceil(n)
Ex:
ceil(5.7) =>
6
floor(n) - It returns a value rounded
down to the next lower integer.
Syntax:
floor(n)
Ex:
floor(5.7) =>
5
ceil(n)
|
>>>>
|
6
|
100
|
-67
|
8
|
1
|
‘n’
|
>>>>
|
5.7
|
99.0001
|
-66.23
|
8
|
0.004
|
floor(n)
|
>>>>
|
5
|
99
|
-66
|
8
|
0
|
sin( ) - This function is used to find trigonometric SINE value of given angle in radian.
Syntax:
sin(n angle in radian)
Ex:
sin(90) =>
1
~~~~~~~~~~~~@~~~~~~~~~~~~
// An
example program for any of the arithmetic function.
#
include<stdio.h>
#
include<conio.h>
#
include<math.h>
void
main( )
{
clrscr();
printf(“Square Root = %f“, sqrt(20));
}
~~~~~~~~~~~~@~~~~~~~~~~~~
// An
example program for any of the arithmetic function.
#
include<stdio.h>
#
include<conio.h>
#
include<math.h>
void
main( )
{
int R= fmod(7, 2);
clrscr();
printf(“Remainder = %d“, R);
}
~~~~~~~~~~~~@~~~~~~~~~~~~
// Testing
Even – odd using fmod. EO_FMOD.C
#
include<stdio.h>
#
include<conio.h>
#
include<math.h>
void
main( )
{
int N;
clrscr();
printf(“Input a Number = “);
scanf(“%d”, &N);
if(fmod(N, 2)==0)
{
printf(“EVEN Number”);
}
else
{
printf(“ODD Number”);
}
printf(“\n
Good Day”);
}
~~~~~~~~~~~~@~~~~~~~~~~~~
// WAP
to Calculate Simple and Compound Interest. SICI.C
#
include<stdio.h>
#
include<conio.h>
#
include<math.h>
void
main( )
{
int P, T;
float R, SI, CI;
clrscr( );
printf(“Input Principal Amount = “);
scanf(“%d”, &P);
printf(“Input Rate = “);
scanf(“%f”, &R);
printf(“Input Time = “);
scanf(“%d”, &T);
SI=P*R*T/100;
CI = P *
pow((1+R/100)
, T) – P;
printf(“\n Simple Interest = %f”, SI);
printf(“\n Compound Interest = %f”, CI);
}
~~~~~~~~~~~~@~~~~~~~~~~~~

No comments:
Post a Comment