Why Use Function
Rules Of Functions
Function Prototypes
Library functions
User-defined function
Function Definition
Function Call
Function Declaration
Function Declaration and Prototypes
Call by reference & Call by Value
Recursion (Recursive function)
Introduction:
Function in C can perform a particular task and supports the concept of modular programming design techniques. It is a self-contained block of statements that perform a coherent (large & difficult) task of some kind. Every C program can be thought of as a collection of these functions. C program is a collection of one or more functions. A function gets called when the function name is followed by a semi-colon.
The main( ) function is a specially recognized function in C. Every program must have a main function to indicate where the program has to begin its execution. While it is possible to code any program utilizing only main( ) function.
Compiler always begins the program execution with main( ), every function in a program must be called directly or indirectly by main( ). In other words, the main( ) function drives other functions.
Syntax:-
}
void function_name( ); // Function Declaration…….
void main( )
{
----------------------
-----------------------
function_name( ); // Function Calling…………
------------------------
------------------------
}
function_name( ) //
Function Definition…….
{
-----------------
-----------------
}
Why
Use Function:
Ø
Writing functions avoid
re-writing the same code over and over.
Ø
If the operation of a
program can be divided into separate activities and each activity placed in a
different function, then each could be written and checked more or less
in-dependent.
Ø
Separating the code into
modular functions also makes the program easier to design and understand.
Ø
Using functions it becomes
easier to write programs and keep track of what they are doing.
Rules
Of Functions:
Ø
C program is a collection of
one or more functions.
Ø
A function gets called when
the function name is followed by a semicolon.
void main( )
{
---------------------
function_name( );
----------------------
}
Ø
A function is defined when
function name is followed by a pair of braces in which one or more statements
may be present.
function_name( )
{
statement1;
statement2;
}
Ø
Any function can be called
from any other function. Even main( )
can be called from other functions.
main( )
{
---------------------
function_name( );
----------------------
----------------------
}
function_name( )
{
statement( );
main( );
------------------
------------------
}
Ø
A function can be called any
number of times.
Ø
The order in which the
functions are defined in a program and the order in which they get called need
not necessarily are same.
Ø
A function can call itself.
Such a process is called “recursion”.
Ø
A function can be called
from other function, but a function cannot be defined in another function.
Function
Prototypes:
A function prototype is a function declaration which includes
information and types of the arguments that the function takes. A declaration
without any information about the arguments is not a prototype. A function is
declared with a prototype. Function prototypes are abstract function
interfaces. These function declarations have no bodies with their interfaces.
Function prototypes are usually declared at the top of a C source file or in a
separate header file.
A declaration or prototype is a way of telling the compiler the
data types of the any return value and of any parameters, so it can perform
error checking.
The importance of prototyping: -
Ø
It makes program more
structured and easier way to read code.
Ø
It allows the C compiler to
check the syntax of function calls.
Functions are
basically of two types:-
1.
Library functions. (In-built
function / pre-defined function)
2.
User-defined function.
1.
Library functions:-
These functions are also
called In-built function or pre-defined function. These functions are built in the C library. User uses it according to
their requirements.
e.g: - printf( ), scanf( ),
clrscr( ), getch( ) etc.
2.
User-defined function:-
These functions are built by
user according to program’s requirements.
e.g: - sum( ), sub( ), total( ) etc.
Note: - main( ) function is a logic and
predefined function.
int total (int x, int y, int z);
void main( )
{
int a, b, c, sum;
printf ("Enter any three numbers : \n
");
scanf ("%d %d %d", &a, &b, &c);
sum= total
(a,b,c);
printf ("Sum= %d", sum);
}
int total (int x, int y, int z)
{
int d;
d=x+y+z;
return (d);
}
Ø Value of a, b, c in function main( ) are passed on the function
total( ) by converting the x, y and z value.
Ø
a, b, c are called ‘Actual
arguments/parameters’.
Ø
x, y, z are called ‘Formal
arguments/parameters’.
Ø
Actual arguments and formal
arguments must always be same.
Ø
Methods of declaring the
formal arguments.
§ total (int x, int y, int z);
§
total (int, int, int);
Ø
Return the sum of x, y, z
and then we use the return statement.
Ø
Return statement is used for
two purposes:
§
On executing the return
statement it immediately transfers the control back to the calling program.
§
It returns the value present
in the parentheses after return, to the calling program. In the above program the
value of sum of three numbers is being returned.
Ø
A function can return only
one value at a time. Thus, the following statements are in-valid statements:-
§
return (a, b);
§
return (x, 12);
Ø
Valid return statements:-
§
return (a);
§
return (23);
§
return (12.32);
Ø
If the value of a formal
argument is changed in the called function, the corresponding change does not
take place in the calling function.
Example:-
void sum (int b);
void main( )
{
int a=30;
show (a);
printf ("\n A= %d", a);
}
void show (int b)
{
b=60;
printf ("B= %d", b);
}
Output:- B=60
A=30
1.
Function Definition
2.
Function Call
3.
Function Declaration
1.
Function Definition:-
Function definition is also known as function implementation. They are following elements: -
I.
Function name
II.
Function type
III.
List of parameters
IV.
Local variable declaration
V.
Function statement
VI.
A return statement
All the six elements are grouped into two parts, namely,
Ø
Function Header (1 to 3
elements)
Ø
Function Body (4 to 6
elements)
Syntax:-
functionType function_name (List of parameters)
{
// local variable
// statements
// return
statement
}
2.
Function Call:-
A function can be
called by simply using function name followed by a list of actual arguments (parameters), if any,
enclosed in parentheses.
Syntax:-
main( )
{
message( ); // Function
Call….
}
3.
Function Declaration:-
All functions in a C program must be declared, before they are
invoked. A function declaration (also known as function prototype) consists of
four parts.
I.
Function type (return type)
II.
Function name
III.
Parameter list
IV.
Terminating semicolon
1.
Function Declaration and Prototypes
2.
Calling function by value or
by reference
3.
Recursion (Recursive
Function)
1.
Function Declaration and Prototypes:-
Any C function by default return an int value. A call is made to a
function, the compiler assumes that this function would return a value of type
int.
Example:-
int square (float);
void main( )
{
float a, b;
printf ("Enter any number = ");
scanf ("%f", &a);
b= square (a);
printf ("\n Square of %f is %f", a, b);
}
int square (float x)
{
float y;
y=x*x;
return (y);
}
Output:- Enter
any number = 3
Square of 3.0000 is 9.0000
Ø
If we want that a called
function should not return any value. This is made possible by making use of
the void keyword.
Example:-
void display ( );
void main( )
{
display ( );
}
void display ( )
{
printf ("Void is not returning a
value. ");
printf ("\n Int is returning a value. ");
}
Output:-
Void is not returning a value.
Int
is returning a value.
2.
Call by Value & Call by reference:-
The function calls are
called “Calls by value” when on
calling a function, we are passing values of variables to it.
We have also learnt that
variables are stored somewhere in memory. So instead of passing the value of a
variable, can we not pass the location number (also called address) of the
variable to a function? If we are able to do so it would become a “Call by reference”.
Function has two type of argument:
Functions with Pointer:-
Example:-
void main( )
{
int i=3;
printf ("Address of i= %u", &i);
printf ("\n value of i=%d", i);
}
Output:- Address of i=6485
Value of i=3
Look at the first printf ( ) statement carefully, ‘&’
used in this statement is C’s ‘Addressof’
operator. The expression &i
return the address of variable i,
which in this case happens to be 6485. Since 6485 represents an address. It prints
out using %u, which is a format
specifies for printing an unsigned integer. We have been using the ‘&’ operator all the time in the scanf ( ) statement.
The other pointer operator
available in C is ‘*’, called ‘value at
address’ operator. It gives the value stored at a particular address. The ‘value at address’ operator is also
called ‘indirection operator’.
Example:-
void main( )
{
int i=3;
printf ("Address of i=%u", &i);
printf ("\nValue of i=%d", i);
printf ("\nValue of i=%d", *(&i));
}
Output:- Address
of i=6485
Value of i=3
Value of i=3
Note:- printing the value of
*(&i) is same as printing the value of i.
The expression &i gives
the address of the variable i. This address can be collected in a variable, by
saying:-
j= &i;
But remember that j is not
an ordinary variable like any other integer variable. It is a variable which
contains the address of other variable. Since j is a pointer variable so
compiler must provide it space in the memory.
Example:-
void main( )
{
int i=3;
int *j, **k;
j=&i;
k=&j;
}
Ø Arguments can generally be passed to functions in one of the two ways:-
§
Sending the values of the
arguments.
§
Sending the address of the
arguments.
Ø
In the first method, the ‘value’ of each of the actual arguments
in the calling function is copied into corresponding formal arguments of the
called function. With this method
the changes made to the formal arguments in the called function have no effect
on the values of actual arguments in the calling function.
Example:-
void swap (int x, int y);
void main ( )
{
int a=10, b=20;
swap (a, b);
printf ("A= %d \n B=%d", a, b);
}
void swap (int x, int y)
{
int
t;
t=x;
x=y;
y=t;
printf
("X= %d", x);
printf
("\nY= %d", y);
}
Output:- X=20
Y=10
A=10
B=20
Note:- values of a and b remain unchanged
even after exchanging the values of x & y.
Ø
In the second method (Call by reference) the addresses of
actual arguments in the calling function are copied into formal arguments of
the called function. This means that using these addresses we would have an
access to the actual arguments and hence we would be able to manipulate them.
Example:-
void swap (int *x, int *y);
void main( )
{
int a=10, b=20;
swap (&a,
&b);
printf ("A= %d \nB=%d", a, b);
}
void swap (int *x, int *y)
{
int
t;
t=*x;
*x=*y;
*y=t;
printf
("X= %d",* x);
printf
("\nY= %d",* y);
}
Output:- X=20
Y=10
A=10
B=20
Note:- This program is managed to exchange the
values of a and b using their addresses stored in x and y.
Usually in c programming, we
use a call by value function. This means in general you cannot alter the actual
arguments. But if desired, it can always be achieved through a call by reference
function.
Using a call by reference function
intelligently we can make a function return more than one value at a time,
which is not possible ordinarily.
Example:-
void area (int r, float *a, float *p);
void main( )
{
int radius;
float area,
perimeter;
printf ("Enter radius of a circle = ");
scanf ("%d", & radius);
area (radius,
&area, &perimeter);
printf ("Area= %f", area);
printf ("Perimeter= %f", perimeter);
}
void area (int r, float *a, float *p)
{
*a=3.14*r*r;
*p=2*3.14*r;
}
Output:- Enter radius of circle 5
Area= 78.5000
Perimeter=31.4000
Thus, we have been able to
return two values from a called function and hence, have overcome the
limitation of the return statement, which can return only one value from a
function at a time.
1.
Recursion (Recursive function):-
It is possible for the
functions to call themselves. A function is called “Recursive” if a statement within the body of a function calls the
same function. Sometimes called “Circular
definition”, recursion is thus the process of defining something in terms
of itself.
Example:-
Note: In these programs, outputs are same but the approach are
different.
Output:- Enter
any number=3
Factorial Value=6
Ø If you don’t use iterative methods like for, while or do-while
loop.
FLOW CHART OF PROGRAM
Final value will be returned back to main function. So in this way, recursive process takes place.
According
to above example, we input 3 as value of x.
1) So
first it will check whether the value of x is equal to 1. But in first call of
rec( ) function, it will go to else statement and execute f=x*rec(x-1);
statement means f=3*rec(3-1).
2) So
in 1st step, it will again call rec( ) function to calculate value
of rec(2). Now value of x is 2, which is
not equal to 1 so it will again execute f=x*rec(x-1); statement means f=2*rec(2-1).
3) So
in 2nd step it will again check for x==1 but now value of x is 1.
4) So
in 3rd step it will return 1
to previous function and in step 4, value of f will be returned to previous
function again.
Final value will be returned back to main function. So in this way, recursive process takes place.
0 comments:
Post a Comment