A function definition has two principal components:the first line (including the argument declarations), and the body of the function.
The first line of a function definition contains a type specification of the value returned by the function,followed by the function name and (optionally) a set of arguments,separated by commas and enclosed in parentheses.Each argument is preceded by its associated type declaration.An empty pair of parentheses 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 (type 1 arg 1,type 2 arg 2,.........,type n arg n)
where data-type represents the data type of the item that is returned by the function, name represents the function name and type 1,type 2,.......,type n represent the data types of the arguments arg 1, arg 2,......,argn.The data types are assumed to be of type int if they are not shown explicitly.
The arguments are called formal arguments, because they represent the names of data items that are transferred into the function from the calling portion of the program.they are also known as parameters or formal parameters.(The corresponding arguments in the function reference are called actual arguments,since they define the data items that are actually transferred. )The identifiers used as formal arguments are "local" in the sense that they are not recognized outside the function.Hence the name of the focal arguments need not be the same as the names of the actual arguments in the calling portion of the program.Each formal arguments must be of the same data type,however, 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 compound statement is sometimes referred to as the body of the function.Like any other compound statement,this statement can contain expression statements,other compound statements,control statements,and so on.It should include one or more return statements,in order to return a value to the calling portion of the program.
In general terms, the return statement is written as
return expression;
A function can access other functions.In fact,it can even access itself (this process is known as recursion.)
Tuesday, December 15, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment