DEFINING A FUNCTION :
A
function has three principal components :
the
first line,
the
argument declarations
the body
of the function
The first
line of a function definition contains the byte specification of the value
returned byte the function, followed by the function name and (optionally) a
set of parameters separated by comas and enclosed in parenthesis. The type
specification can be omitted if the function returns an integer or a character,
in entry pair of parenthesis must follow the function name if the function
definition does not include any arguments. In general terms, the first line can
be written as :
Data-type name(formal arg1, arg2, argn)
Where
data type represents the data type of the value, which is returned, and name
represents the function name.
The
formal argument allows information to be transferred from the calling portion
of the program to the function. They are also known as parameter on formal
parameters.(the corresponding arguments in the functions reference are called
actual arguments since they define the information actually being transferred.
The identifiers used as formal argument are local in the sense that they are
not recognized outside of the function. Hence, the names of the formal
arguments may be same as the names of the other identifiers that appear outside
the function definition.
The
argument declaration follows the first time. All format arguments must be
declared at the point in the function. Each format argument must have the same
data type as its corresponding actual arguments. That is each formal argument
must be of the same data type as the data items it receives from the calling
portion of the program.
The
remainder of the function definition is a compound statement that defines the
action to be taken by the function. This command statement is sometimes
referred to as the body of the function. It must follow the formal argument
declarations. Like any other compound statements, the statement can contain
expression statements. Other compound statement, control statements, and so on.
As a result it can even access itself. This process is known as recursion.
For example,
The first line contains the title,
low-to-up followed by the formal argument cl, within is enclosed n parameters.
Note that g1 represents the lowercase character that is transferred to function
from the calling portion of the program.
Notice
that this function returns a character content which actually an integer value.
Therefore, a type specification is not required in the first line. It would be
permissible, however, to write the first line as
Char low-to-up(c1)
The first
formal argument c1 is declared to be a char – type. The body of the function
follows this. Which begins on the third line. Notice that the body contains the
declaration of the local char-type variable c2. following the declaration of a
statement that tests whether c1 represents a lowercase letter. Finally, the
program information is returned from the function to the calling portion of the
program via the return statement. The return statement also causes control to
be returned to the point from which the function was accessed.
In
general terms, the return statement is written as return(expression);
The value
of the expression is returned to the calling portion of the program. The
expression is optional. However, a return statement can be written without it.
If the expression is omitted , the return statement simply causes control to
never back to the calling porting of the program, without any information
transfer.
Now let
us momentarily return to the function type specification, which appears in the
first line of the function definition. This type specification can be omitted
if the function returns an integer quantity or a character. If the function
returns different type of quantity, such as a long integer, floating point or
double precision quantity, however, the first line of the function definition
must include a corresponding type specification.
Now
suppose the data type specified in the first line is inconsistent with the
expression that appears in the return statement. Under such conditions the
compiler will attempt to convert the quantity to be represented by the
expression that the data type specified in the first line. This could result in
a compilation error, as it might in value a partial loss of data.
ACCESSING A FUNCTION :
A
function can be accessed (i.e. called) by specifying its name, followed by a
list of arguments enclosed in parentheses and separated by commas. If the
function call does not require any arguments, an empty pair of parenthesis must
follow the function’s name. The function call may appear to itself (that is, it
may comprise a simple expression), and it may be one of the operands within a
more complex expression.
The
arguments appearing in the function call are referred to as actual arguments.
In contrast, to the formal arguments that appear in the first line of the
function definition.(they are also known simply as arguments or as actual
parameters). In a normal function call there will be one actual argument for
each formal argument. The actual arguments may be expressed as constants,
single variables, or more complex expressions. However, actual argument must be
of the same type as its corresponding formal arguments.
There may
are several different calls to the same function from various places within a
program. The actual argument may differ from one function call to another,
within each function call, however, the actual argument must correspond to the
formal arguments in the function definition.; i.e. the number of actual
argument must be same as the number of formal argument and each actual
arguments must be of the same data as its corresponding for argument.
If a
function returns a non-integer quantity and the portion of the program
containing the function call precedes the function definition, then there must
be a function declaration in the calling position of the program. This is
sometimes referred to as a forward declaration. The function declaration
effectively informs the compiler that a function will be accessed before it is
defined.
In its simplest
form, a function declaration can be written as,
Data-type name();
Where
data-type refers to the data type of the quantity returned by the function and
name refers to the function name.
Function
calls can span several levels within a program; function A can call function B
which can call function C and so on. Also function a can call function c
directly, and so on.
PASSING ARGUMENT TO A FUNCTIN :
When a
single value is passed to a function via an actual argument. The value of
actual argument is copied to the function. Therefore, the value of the
corresponding formal argument can be altered within the function. But the value
of the actual argument within the calling routine will not change. This
procedure for passing the value of an argument i/o a function is known as
passing by value.
Passing
an argument by value has advantages and disadvantages on the plus. It allow a
single-value used actual argument to be written as an expression rather than,
being restricted to a single variable. Moreover, if the actual argument is
expressed as a single variable. It prevents information from being transferred
back the calling portion of the program via arguments. Thus, passing by value
is restricted to a one-way transfer of information.
Array is
passed differently than single-valued entities. If an array name is specified
as an actual argument the individual array elements are not copied to the
function. Instead, the location of the array (i.e. the location of the first
element) is passed to the function. If an
element of the array is accessed within the function; the access will refer to
the location of the array element relative the location of the first element.
Thus any alteration to an array element within the function will carry over to
the calling routine.
SPECIFYING ARGUMENT DATA TYPES ;
An
additional reason for including a function declaration within the calling
portion of a program it is possible to include the data type of the arguments
within the function declaration. The compiler will then convert the value of
each actual argument to the declared data type ( if necessary ) and then
compare each (converted) actual data type with the corresponding formal
function declarations will allow the programmers to be informal of data type,
inconsistencies detected during the compilation process.
When the
argument data types are specified in a function declaration; the general form
of the function declaration can be written as :
Data-type name(argument type1, argument
type2,… argument typen);
Where
data type represents the data type of the quantity returned by the function,
name represents the function name and argument type 1 argument type 2 …
argument type n refers to argument data types of the first argument, second
argument and so on. Remember that the argument
data type are optional even in the situations that requires a function
declaration. Most C compilers supports the use of the keyword void in function
definition as a return data type indicating that the function does not return
anything. Function declaration may also include void for the same purpose. In
addition void may appear in an argument list, in both function definition and
function declarations, to indicate that a function does not require arguments.
In the later case, void appears by itself in the area normally used for
argument specifications.
CATEGORY OF FUNCTION :
A
function, depending and where argument are present or not and where a value is
returned or not, may belong to one of the following categories.
No arguments and not return values:
When a
function has no arguments, it does not receive a data from the calling
function. Similarly, when it does not return a value, the calling function does
not receive any data from calling function. In fact, there is not data transfer
between the calling function and the called function.
We would
ensure that the function call has matching arguments. In case, the actual
arguments are more than the formal arguments, extra actual arguments are
discarded. On the hand, if the actual arguments are less than the formal
arguments, the unmatched formal arguments are initialized to some garbage
values. Any mismatch in data type may also result in passing of garbage value.
Remember no error message will be generated.
While the
formal arguments must be void variable names; the actual arguments may be
variable name expression or constants. The variable used in actual arguments
may be assigned values before the function call is made.
Arguments with return values :
When
function is called with arguments, then called function will receive the
arguments. Then the statement included within the body at the called function
will be executed.
RECURSION :
Recursion
is a process by which a function calls itself repeatedly, until some specified
condition has been satisfied. The process is used for repetitive computation in
which each action is stated on forms of previous result. The process is used
for repetitive computation in which each action in terms of a previous result.
Many iterative problems can be written in this form.
In order
to solve a problem recursively, two condition must be satisfied. First, the
problem must be written in recursive form and second the problem example, we
wish to calculate the factorial of a positive integer quantity. We would
normally express this problem as n1=1 x 2 x 3 x 4 … x n where n is the
specified positive integer. However, we can also express in another way, by
writing n=1 = n*(n-1). This is a recursive statement of the problem, in which
last expression provides a stopping condition for the recursion.
When a
recursive program is executed the recursive function calls are not executed
immediately. Rather, they are placed on a stack until the condition that
terminates the recursion is encountered. The function calls are then executed
in reverse order, as they are popped off the stack.
If a
recursive function contains local variables, a different set of local variables
will be created during each call. The name of the local variables, will be
cause always be the same, as declared within the function. However, the
variables will represent a different set of values each time the function is
executed. Each set of values will be stored on the stack, so that they will be
available as the recursive process “unwinds” i.e. as the various function calls
are “popped” off the stack and executed.
THE SCPOE AND LIFETIME OF VARIABLES IN FUNCTION :
A
variable in C can have any one of the four storage classes :
Automatic variables :
Automatic
variables are declared inside a function in which they are to be utilized. They
are created when the function is called and destroyed automatically when the
function is exited, hence the name automatic. Automatic variables are therefore
private to the function in which they are declared. Because of this property,
automatic variables are also referred to as local or internal variable.
A
variable declared inside a function without storage class specifications, by
default, an automatic number in the example below is automatic.
One
important feature of automatic variable is that their value cannot be changed
accidentally. This assures that we may declare and use the same variable name
in different function in the same program without causing any confusion to the
compiler. There two consequences of the scope and longevity of auto variables.
First any variable local to main will normally live throughout the whole
program, although, it is active only in main. Secondly, during recursion, the
nested variables are unique auto variables, a situation similar to function, nested
auto variable with identical names.
Automatic,
variable can also be defined within a set of braces known as “blocks” they are
meaningful only inside the block where they are defined.
External variables :
Variables
that are both alive and active throughout the entire program are known as
external variables. They are also known as global variables. Unlike function in
the program external variables are declared outside a function. For example the
external declaration of integer number and float length might appear is :
Int number;
Float length = 7.5;
Main()
{
---
---
}
function1()
{
---
---
}
function2()
{
---
---
}
the
variables number and length are available for use in all the three functions.
In case a local variable and a global variable have the same name, the local
variable will have preceded over the global variable in the function where it
is declared.
Once a
variable has been declared as global, any function can use it and change its
value. Then subsequent function can reference only that new value. Because of
this property, we should try to use global variables only for tables of for
variables shard between functions when it is inconvenient to pass them as
parameters.
One other
aspect of a global variable is that it is visible only form the point of declaration to the end of the program.
Note that
the extern declaration does not allocate storage space for variables. In case
of arrays, the definition should include their size as well.
An extern
within a function provides the type information to just that one function. We
can provide type information to all function within a file by placing external
declaration before any of them, as shown below.
---
printout();
}
void
printout ()
{
int
t;
---
---
}
float height(size);
0 comments:
Post a Comment