DEFINITION OF FUNCTION
Organizing
a large program into small, independent program segments called
functions. It is an independent program that is written to perform a
specific task.
Functions are identified and designed such that they can be organized into a top down hierarchical structure.
ELEMENTS OF USER USER-DEFINED FUNCTIONS
A user-defined function consists of 3 elements that are related to functions
- FUNCTION DEFINATION
- FUNCTION CALL
- FUNCTION DECLARATION
Function definition
It
is an independent program function that is specially written to perform
a specific task. In order to use a function that we need to invoke it
at a required place in the program. This is known as the function call. The program that calls the function is referred to as the calling program or calling function. The calling programs should declare any function that to be used later in the program. This is known as the function declaration or function prototype
A function definition include the following elements:
- function name
- function type
- list of parameters
- local variable declarations
- function statements and
- a return statement
All six elements are grouped into 2 parts, namely,
- function header
- function body
A general format of a function definition :
return_type function_name (parameter list)
{
local variable declaration;
executable statement1;
executable statement2;
. . . . . . . .
. . . . . . . .
return statement;
}
|
The 1st line return_type, function_name (parameter list) is known as the function header and statements within the opening and closing braces constitute the function body. Which is a compound statement.
Function Header
The function header consists of 3 parts;
* return type
* function name
* formal parameter list
* return type
* function name
* formal parameter list
Return type :
The return type specifies
the type of valve that the function is expected to return to the
program calling the function. If the function type is not explicitly
specified, C will assume that it is an integer void type. If the
function is not returning anything, then we need to specify the function
type as void.
Function name : is any valid C identifier and therefore follows the same rules of formation as other variable names In C
Formal Parameter List :
Parameter
list declares the variable that will receive the data sent by the
calling program. They serve as input data to the function to carry out
the specified task.
They represent actual input valves; they are often referred to as formal parameters. The parameters are also known as arguments.
The parameter list contains declaration of variables separated by commas and surrounded by parameters.
Example;
float quadratic (int a, int b, int c,);
|
a
function need not always receive valves from the calling program. in
such cases , functions have no formal parameters. To indicate that the
parameter list is empty, we use the keyword void between the parentheses as in
void printline (void)
{
. . . . . .. .
}
|
FUNCTION BODY:
If
a function does not return any value, we can omit the return statement.
However, note that its return type should be specified as void.
void mul (float x, float y)
{
float results; /* local variable */
result = x * y; /* computes the product */
printf(“%d”, result); /* prints the result */
}
|
If a function does return any value, it then should contain the return statement and specify the return type
float mul (float x, float y) /*function definition with formal parameters */
{
float results; /* local variable */
result = x * y; /* computes the product */
return (result); /* returns the result */
}
|
FUNCTION CALLS: A function can be called by simply using the function name followed by list of actual parameters. If any , enclosed in parentheses
Example:
main( )
{
int y;
y = mul(10,5); /*function call with actual arguments */
printf(“%d \n”,y);
}
|
· When
the compiler encounters the function call, the control is transferred
to the function mul( ). This function is then executed and the control
returns with or with out a value.
· There are many different ways to call a function. Listed below are some of the ways the function can be invoked:
Mul(10,5)
Mul(m,5)
Mul(10,n)
Mul(m,n).
FUNCTION DECLARATION OR PROTOTYPE:
· All functions in C program must be declared,before they are invoked. A function declaration consists of 4 parts:
- Return type
- Function name
- Parameter list
- Terminating semi colon.
Example:
int mul (int m, int n); /*function prototype*/
|
· A prototype declaration may be placed in two places in a program.
o Above all the functions (including main).
o Inside a function definition.
· When we place the declaration above all the functions(in the global declaration section),the prototype is referred as a GLOBAL PROTOTYPE.
· When we place it in a function definition, the prototype is called a LOCAL PROTOTYPE.RECURSIVE FUNCTIONS
· When a called function in turn calls another function a process of ‘chaining’ occurs.
· Recursion is a special case of this process, where a function calls itself
· For example: factorial of 4 = 4*3*2*1 = 24
A function to evaluate factorial of n is as follows
Factorial (int n)
{
int fact;
if (n==1)
return (1);
else
{
fact = n* factorial (n-1);
return(fact);
}
}
|
Let us see how the recursion works. Assume n=3. Since the valve of n is not 1, the statement
Fact = n * factorial(n-1);
Will be executed with n =3. That is
Fact = 3 * factorial(2);
Will be evaluated. The expression on the right-hand side includes a call to factorial with
n = 2. This will return the following valve;
2 * factorial(1)
Once again, factorial is called with n =1. This time, the function returns 1. The sequence of operations can be summarized as follows:
Fact = 3*factorial(2)
= 3*2 factorial (1)
=3*2*1
=6
Recursive functions can be effectively used to solve problems where solution is expressed in terms of successively applying the same solution to subsets of the problem. When we write recursive functions, we must have an if statement somewhere to force the function to return without the recursive call being executed. Otherwise, the function will never return.
What are formal and actual parameters ?
· The parameters used in prototypes and function definitions are called formal parameters and those used in function calls are actual paramters.
· Actual parameters used in a calling statement may be simple constants, variables , or expressions.
Ex: Mul(10,5)
Mul(10,n)
Mul(m,n)
· The formal and actual parameters must match exactly in type, order and number. Their names, however, do not need to match.
Example:
int
mul(int x, int y); // formal parameters in
function prototype
main( )
{
int
a=10,b=5;
y=mul(a,b); /* actual parameters in function calling
statement*/
}
int
mul(int x, int y) /* formal parameters in function
definition*/
{
int p;
p=x*y;
return (p);
} |
Passing arrays to functions:
One dimensional array :
To pass a one dimensional array to a called function, it is sufficient to list the name of the array(without any subscript) and the size of the array as argument.
For example
int arry[10],n; /*array declaration*/
----
----
largest(arry, n); /* function call st. - passing array as actual argument*/
|
The function call largest (arry, n) , Will pass the whole array arry to the called function. The called function expecting this call must be approximately defined.
The largest function header might look like
int largest(int array[], int size) /* function definition. - array as formal argument*/
|
The pair of brackets informs the compiler that the arguments array is an array of numbers. It is not necessary to specify the size of the array here.
Main()
{
int largest (float arry[], int n);
int value[4]={12, 25, 2, 67};
printf(“%d\n”,largest(value,4));
}
int largest(int arry[],int n)
{
int i;
int max;
max=a[0];
for(i=1;i<n;i++)
if(max<a[i])
max=a[i];
return(max);
}
|
Consider a problem of finding the largest value in an array of elements.
When the function call largest( value,4) is made, the values of all elements of array value become the corresponding elements of array a in the called function. The largest function finds the largest value in the array and returns the result to the main.
By passing the array name, we are infact, passing the address of the array to the called function. The array in the called function now refers to the same array stored in the memory that is created from the main. Therefore, any changes made in the array in the called function will be reflected in the original array of the main.
Passing address of parameters to the functions is referred to as pass by address.