Wednesday, 18 May 2011

SOLUTION: CHAPTER- 09: (User defined Function) from PROGRAMMING IN ANSI C: E BALA GURUSHAMY

User-Define Function

REVIEW QUESTION:

Question: State whether the following statements are true or false

(a) C function can return only one value under their function name.

Answer: False

(b) A function in C should have at least one argument.

Answer: True

(c) A function can be defined and placed before the main function.

Answer: False

(d) A function can be defined within the main function.

Answer: False.

(e) An user-defined function must be called at least once; otherwise

a warming message will be issue

Answer: True.

(f) Any name can be used as a function name.

Answer: False.

. (g) Only a void type function can have void as its argument

Answer: False.

(h) When variable values are passed to function, a copy of them are

created in the memory.

Answer: True.

(i) Program execution always begins in the main function irrespective of location in the program.

Answer: True.

(j) Global variable are visible in all blocks and function in the program

Answer: False.

.

(k) A function can call itself.

Answer: True

(l ) A function without a return statement is illegal.

Answer: False.

(m) Global variable can not be declared as auto variables.

Answer: False.

(n) A function prototype must always be placed outside the calling function.

Answer: True

(o) The return type of a function int by default.

Answer: True.

(p) The variable names used in prototype should match those used in the function definition.

Answer: True.

(q) In parameter passing by pointers, the formal parameters must be prefixed with the symbol * in their declarations.

Answer: True.

(r) In parameter passing by pointers, the actual parameters in the function call may be variables or constants.

Answer: False.

(s) In passing arrays to function, the function call must have the name of the array to be passed without brackets.

Answer: False.

(t) In passing strings to function, the actual parameter must be name of the strings post-fixed with size in brackets.

Answer: True.

Question: Fill in the blanks in the following statements.

(a) The parameters used in a function call are called …………….

Answer: actual parameter.

(b) A variable declared inside a function is called ……….. variable.

Answer: local

(c) By default………….is the return type of a function.

Answer: int

(d) In passing by pointers ,the variable of the formal parameters

must be prefixed with …………….operator in their declaration.

Answer: indirection

(e) In prototype declaration specifying .parameter………... is optional.

Answer: name

(f) …………… refers to the region where a variable is actually variable for use.

Answer: Prototype

(g) A function that calls itself is known as a………….. function.

Answer: recursive

(h) If a local variable has to retain its value between calls to the function, it must be declared as ………...

Answer: void

(i) A data ……….. aids the compiler to check the matching between the actual arguments and the formal ones .

Answer: types

(j) A variable declared inside a function by default assumes …………storage class.

Answer: without

Question: The main is a user-defined function. How does it differ from other user-defined function?

Answer:

The main is an example of user-defined function. Printf and scanf belong to the category of library functions. We have also used other library functions such as sqrt, cos, strcat, etc. The main distinction between these two categories is that library functions are not required to be written by us whereas a user-defined function has to be developed by the user at the time of writing a program.

Question: Describe the two ways of passing parameters to function .When do you prefer to use each of them?

Answer:

The parameter list declars the variables that will receive the data sent by the colling program. They serve as input data to the function to carry out the specified task.Since they represent actual input valus, they are often referred to as formal parameters.These parameters can also be used to send values to the colling programs.This aspect will be covered later when we discuss more about functions.The parameters are also known as arguments.

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

Examples;

float quadratic(int a, int b, int c) {….}

Question: What is prototyping? Why is it necessary?

Answer:

Prototypes enable the compiler to provide stronger type checking, somewhat like that provided by languages such as pascle. When you use prototypes, the compiler can find and report any questionable the conversions between the arguments used to call a function and the type of its parameters. The compiler will also catch differences between the number of arguments used to call a function and the number of parameters in the function. A function prototype consists of four parts.

(1)Function type

(2)Function name

(3)Parameter list

(4)Terminating semicolon.

The general form of a function prototype is function-type function-name (parameter list);

It is a good programming style to declare prototypes in the global declaration section before main. It adds flexibility, provides as excellent quick reference to the functions used in the program, and enhances documentation.

Question: Distinguish between the following:

(a)Actual and formal arguments

Answer:

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

On the other hand, if the actual are less than the formals, the unmatched formal arguments will be initialized to some garbage.

(b) Global and local variables

Answer:

A global variable used in a function will retain its value for future use. A global variable is visible only form the point of its declaration to the end of the program. A local variable is a variable that is defined inside a function and used without having any role in the communication between functions.

(c) Automatic and static variables

Answer:

A variable declared inside a function without storage class specification default , an automatic variable. For instance, the storage class of the variable number in the example below is automatic.

main ( )

{

int number;

-------------

-------------

}

A static variable may be either an internal type or an external type. Depending on the place of declaration. Internal static variables are those which are declared inside a function .Therefore, internal static variables can be used to return values between function calls. For example, it can be used to count the number of calls made to a function.

(d) Scope and visibility variables

Answer:

The scope of variables determine s over what region of the program a variable is actually available for use (active) .on the other hand the visibility refers to the accessibility of a variable form the memory.

(e) & operator and * operator

Answer:

The operator & is called the address operator. On the other hand,

The operator * is known as indirection operator because it gives an indirect reference to a variable through its address.

Question: Explain what is likely to happen when the following situations are encountered in a program

.

(a)Actual arguments are less than the formal arguments in a function.

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

(b) Data type of one of the actual arguments does not match with the type of the corresponding formal argument.

Answer: The formal and actual arguments must match exactly in type, order and number .their names, however, do not need to match.

(a)Data type of one of the argument in a prototype does not match with the type of the corresponding formal parameter in the header line.

Answer: Since the prototype is not available, c will assume that the return type is an integer and that the types of parameters match the formal definitions.

If these assumptions are wrong, the linker will fail and we will have to change the program. The moral is that we must always include prototype declarations preferably in global declaration section.

(a)The order of actual parameters in the function call is different from the order of formal parameters in a function where all the parameters are of the same type.

Answer:

The parameters used in prototypes and function definitions are called formal parameters and those used in function calls are called actual parameters. Actual parameters used in a calling statement may be simple constants , variables or expressions. The formal and actual parameters must match exactly in type , order and number . Their names, however do not need to match.

(a)The type of expression used in return statement does not match with the type of the function.

Question: Which of the following prototype declarations are invalid? Why?

(a) int (fun) void;

Answer: valid

(b)double fun (void)

Answer: double fun (void);

(c) float fun (x,y,n);

Answer: float fun (float x, float y, float n);

(d) void fun (void, void);

Answer: void fun (void);

(e)int fun (int a, b);

Answer: int fun (int a, int b);

(f)fun (int, float, char );

Answer: valid

(g)void fun (int a, int &b);

Answer: void fun (int a, int b);

Question: Which of the following header lines are invalid? Why?

(a) float average ( float x, float y, float z);

Answer: valid

(b) double power ( double a, int n-1)

Answer: double power (double a, double n-1);

(c) int product ( int m, 10)

Answer: int product (int m, int 10);

(d) double minimum ( double x; double y;)

Answer: double minimum (double x, double y);

(e) int mul ( int x , y)

Answer: int mul (int x, int y);

(f) exchange ( int *a, int *b)

Answer: exchange (int *a, int *b);

(g) void sum ( int a, int b, int &c)

Answer: void sum (int a, int b ,int c);

Question: Find errors , if any, in the following function definitions:

( a) void abc ( int a, int b)

{

int c;

.............

return (c);

}

Answer: void abc (int a,int b)

{

int c;

-------------

}

(b) int abc ( int a, int b)

{

....................

........................

}

Answer: int abc (int a; int b)

{

int c;

-----------

return(c);

}

(c) int abc ( int a, int b)

{

double c = a+b;

return (c);

}

Answer: int abc (int a, int b)

{

int c =a+b;

return (c);

}

(d) void abc ( void)

{

...................

....................

return;

}

Answer: void abc (void)

{

int c;

-------------

}

(e) int abc ( void)

{

...........................

............................

return ;

}

Answer: Error in declaration.

Question: Find errors in the following function calls:

(a)void xyz();

Answer:

void xyz ( )

(b)xyx (void);

Answer:

void xyx (void)

(c)xyx (int x, int y);

Answer:

int xyx (int x, int y)

(d)xyzz ();

Answer:

int xyzz( )

(e)xyz ()+xyz();

Answer:

int xyz ( ) + int xyz ( )

Question: A Function to divide two flowing point numbers is as follows:

divide (float x, float y)

{

Return (x/y);

}

What will be the value of the following function calls

(a) divide (10,2)

Answer: 5.000000.

(b) divide (9,2)

Answer: 4.500000.

(c) divide (4.5,1.5)

Answer: 3.000000.

(d) divide (2.0,3.0)

Answer: .6777777.

Question: What will be the effect on the above function calls if we change the header line as follow:

(a) int divide (int x,int y)

(b) double divide(float x, float y)

Question: Determine the output of the following program?

int prod (int m, int n);

main ( )

{

int x=10;

int y=20;

int p, q;

P=prod (x,y);

q=prod (p, prod (x,z));

printf (“%d %d\n”,p,q);

}

int prod (int a,int b)

{

return (a*b);

}

Answer: p=200

q =4000

Question: What will be the output of the following program?

Void test (int *a);

main( )

{

int x=50;

test ( &x);

printf (“%d\n”, x);

}

void test ( int *a);

{

*a=*a +50;

}

Answer: 100.

Question: The function test is coded as follows:

int test ( int number)

{

int m,n=0;

while (number)

{

m= number % 10;

if ( m% 2 )

n=n +1;

number = number/10;

}

return ( n) ;

}

What will be the values of x and y when the following statement are executed

int x = test (135);

ans:x=3

int y= test (246 );

Answer: y=o

Question: Enumerate the rules that apply to a function call.

Answer:

A function call is a postfix expression. The operator (…) is at a very high level of precedence, therefore, when a function call is used as a part of an expression , it will be evaluated first , unless parentheses are used to change the order of precedence.

In a function call, the function name is the operand and the parameters set (..) which contains the actual parameters is the operator . The actual parameters must match the functions formal parameters in type, order and number. Multiple actual parameters must be separated by commas.

Question: Summarize the rules for passing parameters to functions by Pointers.

Answer:

The types of the actual and formal arguments must be same.

The actual arguments must be the addresses of variables that are local to the calling function.

The formal arguments in the function header must b\e prefixed by the indirection operator.

In the prototype the arguments must be prefixed by the symbol *.

To access the value of an actual argument in the called function, we must use the corresponding formal argument prefixed with the indirection operator *.

Question: What are the rules that govern the passing of arrays to functions.

Answer:

The function must be called by passing only the name of the array.

In the function definition, the formal parameter must be an array type, the size of the array does not need to be specified.

The function prototype must show that the argument is an array.

Question: State the problems we are likely to encounter when we pass global variables as parameters to functions

Answer:

Since all functions in a program source file can access global Variables, they can be used for passing values between the functions. However, using global variables as parameters for passing values poses certain problems.

The values of global variables which are sent to the called function may be changed in advariently by the called functions.

Functions are supposed to be independent and isolated modules. This character is lost, if they use global variables.

It is not immediately apparent to the reader which values are being sent to the called function.

A function that uses global variables suffers from reusability.

No comments:

Post a Comment