e.g.
A function sqrt(), that finds the square root ofa given number.
The name of the function is "sqrt", it takes one argument i.e., the number whose square root is to be found, and it return the result, i.e, the square root of the number.
e.g.
A function date(), that finds todays date.
The name is "date" and it does not take any arguments. It returns the result, i.e., today's date.
Thus every function has the following:
a) A name
b) Arguments (optional)
c) return value
In C, functions help in modularizing a program -i.e, forming modules of a large program. A large program is broken down into smaller programs - each called a module. For each module a function is written.
There are two types of functions:
1) Predefined functions :
Functions all ready defined in C header files. To use these functions, only include the header file and call the function.
2) User defined functions :
A programmer creates the user defined functions in C according to need.
For all the library functions given below do the following:
1) State the header file and the prototype of the function
2) Describe the use of the function
3) Give a C program that uses the function.
1) isalnum
2) isdigit
3) islower
4) isupper
5) tolower
6) toupper
7) strcpy
8) strcat
9) strlen
10) strrev
11) strncpy
12) strcmp
13) abs
14) sqrt
15) log
16) log10
17) modf
18) pow
19) sin
20) cos
21) tan
NOTE:
In C, a return type of integer can be used to indicate true or false.
true - any non zero value
false - a zero.
User Defined Functions
A user defined function in C can be created to perform specific tasks. It consists of the following steps:
1) Specifying a function prototype
Specify the signature of the function, ie.
- the return type
- the name
- the argument types of the function
2) Specify the function definition
Specify the steps to be performed by the function.
Both the function prototype and definition must be given outside "main" function.
3) Call the function
The function is called or invoked from within the main() function.
There are mainly 2 types of functions
1) A function which does not return a value, i.e., return type is "void".
2) A function which returns a value.
The arguments passed from main are called as actual arguments and the arguments of the function, where they are collected are called as formal arguments.
Function
Function is one of the most are important facility provided
by C .
There are two types of function
1 library function
2 user defined function
l. library
function
Library function are the function
which are already defined in c
The user can only use these function depending upon the purpose.
He cannot change or modify the meaning or purpose of library
function
Eg
sqrt(
) it is used to find the square root of
a number
a) sqrt(
25 ) à 5.00000
int y =
1600;
sqrt(y)à40.00000
Explain the function e
Explain the functions
Char C1 = ‘a’, c2 = ‘B’
Function Result
topper (c1) A
topper (‘X’) X
topper (‘M’) M
tolower (C2) b
tolower (‘D’) d
tolower (‘e’) e
toascii (C1) 97
toascii (C2) 66
toascii (‘b’) 98
The functions
toupper ( ) & tolower ( ) return a character value whereas toascii ( ) returns
an int. value.
2) User defined
functions (UDF):-
The uses
defined function is the facility by which a user can create his own functions. This provides the modularity approach. i.e. a
big problem can be divided into small sub-problems can be divided into small
sub-problems & every sub-problem will be treated as a separate
problem. By solving every sub-problem we
solve the whole task. Every sub-problem
is known as a module.
Functions
make the task of programming easy, because suppose. We want to perform a task
number of times in a program at different places. Then for this if we want only
main ( ), for every time you have to write the code in the program. This will unnecessarily increase the length
of code & the program becames a big program, then the debugging of this
program becames very difficult.
Another option
is to use functions. We just define the
function for a single time and we can call (Use) it for any number of times,
anywhere in the program. Instead of
handling a big single function, it is better to have no. of small functions.
Related to
functions, the following three things are necessary:-
1) Function
declaration (prototype)
2) Function call
3) Function
Definition.
1) Function
declaration (prototype) :-
Function declaration is the first step
in creating UDF. In ‘C’ it is must that
everything we are using must be declared, first. Without declaring a function we cannot use it. So, it is important that we must declare a
function before using. Function declaration
will be alone only once. i.e. re-declaring a function is not possible. The general format of function declaration is
as follows.
Return type function name (datatype1, arg1, datatype2 arg……, datatypen,
arg.n);
Where, Return type is the type of
value the function will return.
Function
name is the name of the function we want to create. For giving a function-name we must follow the
rules of creating identifies.
Arg1,
arg2, ----, argn are the different arguments or parameters those are passed to the
function. For every argument we must
specify the data type separately.
e.g.
:- 1) int add (int x, int y);
2)
add (int x, int y );
Both
these statements declare a function add, to which two int values are passed and
it returns an int value.
Any
‘C’ function can return at the most only one value more than one values cannot
be return from a function.
To the calling function.
When we don’t specify the return
type compiler assumes that the function will return an int value .
When we don’t want any return value
from function then the return value from function will be void.
Eg void fun1(int x , float y, char z);
This
statement declares a function fun1 to which these parameters are passed of int
type , float type and char type .
When this function ends it returns
nothing to the calling function .
Eg
1. long
int fact(int x);
2. int
add(int x ,int y);
3. void
message (void);
4. float
divide( int x, float y);
function call
Function call statement is necessary for executing a
function because without calling a function we cannot execute it
If the function returns some value, the function return the
same value , the function call statements will be in the form of an expression
.
If it doesn’t return
any value the function call statement will be a simple statement .
While calling a function we don’t specify the return type or
the datatype of arguments.
Eg
1.
message( );
2.
c=add(a,b);
3.
display(c);
function definition
function definition contains the actual code we want to
execute .
every function must be defined independently i.e. every
function must be outside of other
function .
eg
int add
(int x , int y)
{
return(x+y);
}
once you have defined a function you have defined the
function you can call it any no of times
from anywhere in the program.
Depending
upon whether the function returns a value and whether any argument are passed
to that function.
Functions are categorized as follows.
Function with no argument no return value
When a function has no return argument it does not receive any
data from calling function similarly when it does not receive a value the calling function does not
receive any type of data from called function.
i.e. there is no data transfer between calling function and
called function.
main()
{
printf (
"\nl am in main" ) ;
itaty( );
brazil( ):
argentina( );
}
italy( )
{
printf ( "\nl am in italy" ) ;
}
brazil(
)
{
printf ( "\nl am in brazil" ) ;
}
argentina(
)
{
printf ( "\nl am in argentina" ) ;
}
The output of the' above program when executed would be as
under:
I am in main
I am in italy
I am in brazil
I am in araentina
Function with argument
without return value
In this approach the we could make the calling function to
read data from the terminal and pass it on to the called
function.
main( )
{
int i=10;
display( i);
prinf(“value of i in function main
is %d”,i);
getch();
}
display(int x)
{
printf(“\n message from display
function”);
}
Function with
argument & return value
In this approach the function receives data from calling
function through argument and display
the result of calculation are the terminal the function on called transfer the control along which the copies
of value of the actual argument to the called function .
The called function is executed line by line in a normal
manner until the return statement is encountered .After execution of return
statement the value of a return argument is passed to calling function in the
main function.
main()
{
float area;
int radius
= 1 ;
area =
circle ( radius ) ;
printf("\n%f",
area);
}
circle ( int r)
{
float a;
a=3.14*r*r;
return (a);
}
Recursion
Recurtion is a fundamental thing in computer science
Suppose function f calls to procedure p an procedure p again calls to function f then function f is called as recursive
function .
Work through the above program carefully, till you
understand the
logic of the program properly. Recursive factorial function
can be
understood only if you are thorough with the above logic.
Following is the recursive version of the function to
calculate the
fartorial value
main( )
{
int a, fact;
printf ( "\nEnter any number
" ) ;
scant ( "%d", &a );
fact = rec(a);
printf ( "Fartorial vallue = %d",
fact);
}
rec(x)
int x;
{
int f;
if(x==1)
return (1);
else
f=x*rec(x-1);
return (f);
}