Write about Elements of User Defined Function?


In order to make use of  user defined function we need to establish 3 elements that are related to functions.

1. function definition

2. function call

3. function declaration;

The function definition is an independent program module that is specially written to implement the requirements of the functions.

In order to use this function we need to involve 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 call function.

The calling program should declare any function that is to be sued later in the program. This is known as the function declaration ( or) function prototype.

Definition of function:

A function definition, also known as function implementation shall include the following elements.

1. function name

2. function type

3.List of parameters

4. Local variable declaration

5. function statements and

6. A return statement

All the 6 elements are grouped into 2 parts.

function  header ( first 3 elements ) and

function body ( second 3 elements).

Syntax:

function_type function_name ( parameter list)
{
  Local variable declaration;
  Executed statements.
  ;;;;;;;;;;;;;;;;;;
  return statement;
}

The first line in function header and the statements within the opening and closing braces are function body.

Function Header: - 

The function header consists of 3 parts.

The function type ( also known as return type);

The function name and

The formal parameter list.

Note that : a semicolon is not used at the end of the function header.

The function type specifies the type of value (float) that the function is expected to return to the program calling the function.

The function name is any valid C identified and i, must follow the same rules of formation as other variable names in C.

The parameter list declares the variables 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 are often referred to as formal parameters.

The parameter list contains declaration of variables separated by commas and surrounded by parenthesis.

 Ex: int sum ( int a int b)
       {………}

Function body : 

The function body contains the declarations and statements necessary for performing the required task. The body enclosed in branches, contains 3 parts.

1. Local declarations that specify the variables needed by the function.

2. Function statements that perform the task of the function.

3. A return statement that returns the value evaluated by the function.

It a function does not return any value, we can omit the return statement however note that its return type should be specified as void.

Return values and their types:

A function may or may not send back any value to the calling function. If it does, it is done through the return statement.

The called function can only return one value per call.

The return statement can take one of the following forms:

 return;

The plan return does not return any value it acts much as the closing braces of the function. When a return is encountered, the control is immediately passed back to the calling function.

Ex: if ( error)
       return ;

       return ( exp);

This statement returns the value of the expression.

Ex:

float sum(int a,int b)  // function header

float res;   //local variables declaration

res = x+y;   // function statements

return ( res ); //return statement

II. function calls:

A function can be called by simply using the function name followed by a list of actual parameters, enclosed in parentheses

Ex:

main ( )
{
int y;
y = mal ( 10,20);  //call
}

When the compiler encounters a functional call, the control is transferred to the sub function.

This function is then executed line by line and a value is returned.

Actual parameters are must match with formal parameters in type, order and number.

If actual parameters are more than formal parameters the extra actual arguments will be discarded.

If actual are less than formals, the unmatched formals will be initialized to some garbage.

III. Functional declaration:

Like variables all functions in a C program must be declared, before they are invoked. A function declaration (also known as function prototype) consists of 4 parts.

Function type ( return type)

Function  name

Parameter list

Terminating semicolon

Syntax: Function_ type Function_name ( Parameter list);

This is very similar to the function head line except the terminating semicolon.

A prototype declaration may be placed in 2 places in a program.

1. Above all the functions ( including main )

2. Inside a function definition.

Points to note:

The parameter list must be separated by commas.

The parameter names do not need to be the same in the prototype declaration and the function definition.

The type must match the types of parameters in the function definition, in number and order.

Use of parameter names in the declaration is optional.

If the function has no formal parameters, the list is written as (void).

The return type is optional, when the function returns in type data.

The retype must be void if no value is returned.

Related

C Language 2449790776284248398

Post a Comment

emo-but-icon

item