Computer Science, asked by urooj123, 10 months ago

A program uses a function named convert() in addition to its main function. The function main() declares the variable x within its body and the function convert declares two variables y and z within its body, z is made static. A fourth variable m is declared ahead (ie at top) of both the functions. State the visibility and lifetime of each of these variables.

Answers

Answered by akm26381
1

Answer:

Explanation:

Motivation and Benefits

Very often in computer programs there is some code that must be executed multiple times in different places in the program.

It is also common to need the same code in multiple different programs.

Encapsulating frequently-used code into functions makes it easy to re-use the code in different places and/or different programs.

Separating code out into functions also allows the programmer to concentrate on different parts of the overall problem independently of the others.

In "top-down" programming a programmer first works on the overall algorithm of the problem, and just assumes that functions will be available to handle individual details.

Functions can then be concentrated on independently of the greater context in which they will be used.

Well-written functions should be general enough to be used in a wide range of contexts, not specific to a particular problem.

Functions can be stored in libraries for later re-use. Examples of functions we have used include log( ), sqrt( ), abs( ), cos( ), etc.

Syntax and Components

The general syntax of a function is as follows:

return_type function_name( arg_type argument, ... )  {

        local_variable_type  local_variables;

        executable  statement(s);

        return  return_value;

    }

Example:

int add( int a, int b ) {

        int sum;

        sum = a +  b;

        return  sum;

    }

 

Return Type

The "return type" indicates what kind of data this function will return.  In the example above,the function returns an int.  

Some languages differentiate between "subroutines", which do not return a value, and "functions", which do.  In C there are no subroutines, only functions, but functions are not required to return a value.  The correct way to indicate that a function does not return a value is to use the return type "void".  ( This is a way of explicitly saying that the function returns nothing. )

If no return type is given, the compiler will normally assume the function returns an int.  This can cause problems if the function does not in fact return an int.

Function Name

The function name is an identifier by which this function will be known, and obeys the same naming rules as applied to variable names ( Alphanumeric characters, beginning with alpha, maximum 31 significant characters, etc. )

Formal Parameter List

Following the function name are a pair of parentheses containing a list of the formal parameters, ( arguments ) which receive the data passed to the function.

The ANSI standard requires that the type of each formal parameter to be listed individually within the parentheses as shown in the above example.  Even if several parameters are of the same type, each must have its type given explicitly.

If a function takes no parameters, the parameters may be left empty.  The compiler will not perform any type checking on function calls in this case.  A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

Function Body

The body of the function is enclosed within curly {} braces, just as the "main" function with which we have been dealing so far, and contains the instructions that will be executed when this function is called.

The function body starts by declaring all local variables, prior to any executable statements.

In C99 variables can be declared any time before they are used, but in general it is still best to declare all variables that will be used at the beginning of the function, or at the beginning of the block in which they are used for very local variables. ( See scope below. )

There are actually two schools of thought on this issue:

Declaring all variables at the top of the function puts them all together, in one comprehensive list. If you print out the program, then all the variables will print on the same page, making a sort of "checklist" of variables to be taken care of.

Similar questions