0% found this document useful (0 votes)
132 views16 pages

User Defined Functions - Review Question Solution

C programming

Uploaded by

apurbabarman.mtb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views16 pages

User Defined Functions - Review Question Solution

C programming

Uploaded by

apurbabarman.mtb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Page 92

ANSI C REVIEW QUESTION SOLUTION

User-Defined Functions

9.1: 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.
ANSI C REVIEW QUESTION SOLUTION

(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 use din 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.

9.2: 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
Page 94

(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

9.3 The main is a user-defined function. How does it differ from


other user-defined function?

C function can be classified into two categories .They are library function and
user defined function.
A user defined function has to be developed by the user at the time of writing
a program. Main is an example of user defined functions.
Every program must have a main function to indicate where the program has
to begin its execution.
No other function in the program can be called main.
It is possible to code any program utilizing only main.
Other user define function cannot call main function but main function can
call other user define function.
That is why main function is different user defined functions.

9.4 Describe the two ways of passing parameters to function


.When do you prefer to use each of them?
The technique used to pass data from one function to another is known as
parameter passing. Parameter passing can be done in two ways:

1. Pass by value (also known as call by value).

2. Pass by pointers (also known as call by pointers).

In pass by value, values of actual parameters are copied to the variables in


the parameter list of the called function. The called function works on the
copy and not on the original values of the actual parameters. This ensures
that the original data in the calling function cannot be changed accidentally.
ANSI C REVIEW QUESTION SOLUTION

In pass by pointers (also known as pass by address), the memory


addresses of the variables rather than the copies of values are sent to the
called function. In this case, the called function directly works on the data in
the calling function and the changed values are available in the calling
function for its use.Pass by pointers method is often used when
manipulating arrays and strings. This method is also used when we require
multiple values to be returned by the called function.

9.5: What is prototyping? Why is it necessary?

A function prototype is a basically a declaration for a function. Like


the variables, all function in a program must be declared, before they
are invoked. A function declaration consists of four parts.

1. Function type (return type)


2. Function name.
3. Parameter list.
4. Terminating semicolon.

They are coded in the following format –


function-type function name (parameter list);

If a function is not declared before it is used, C will assume that its details
available at the time of linking. Since the prototype is not available , C will assume 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.

9.6: Distinguish between the following:


(a)Actual and formal arguments

 The parameters used in prototype s and definitions are called formal


parameters.
 The parameter used in function call is called actual parameter.
Actual parameters must match exactly in type, order and number .
But their names , do not need to match.
Example: Suppose sum() is a function.
Page 96

int sum (int x, int y) /* Here x and y are formal parameters

………

void main ()

int ans;

ans= sum (3,5); /* Here 3 and 5 are actual parameters

}
[Note: parameter and argument is same thing]

(b) Global and local variables


 Local (Internal) variables are those which are declared within a
particular function.
 Global (External) variables are declared outside of any function.

Global variable Local variable

Global variable is a variable A variable which is declared inside the main


which is declared before main function or other functions is called local
variable.
function.

A global variable can be accessed A local variable is isolated in its function.


by all functions.
It works with changing values. It does not work with changing values.

Used only one name in a program. Same variable names are working
different values in different
ANSI C REVIEW QUESTION SOLUTION

(c) Automatic and static variables:

Static variables Auto variables

1. We have to specify the storage 1. It is the default storage class.


class to make a variable static.

2. If it is not assigned any value 2. If it is not assigned any value


then it will give 0 as output. then it will give garbage value as
output.

3. It is visible to the block in which it 3. It is visible to the block in which


is declared and also in the function the variable is declared.
where it will passed.

4. It retains its value between 4. It retains its value till the control
different function calls. It holds its remains in the block in which the
last value. variable is declared.

5. Static variable should be 5. Auto variable will compile by the


compiles by compiler first. compiler after the static variable.

6. A static variable maybe either an 6. Auto variable is always internal


internal type or external type type.
depending on the place of
declaration.

(d) Scope and visibility of variables:

Scope:

The region of a program in which a variable is available for use.

Visibility:

The program’s ability to access a variable from the memory.


Page 98

(e) & operator and * operator

The operator & is called the address operator.


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

9.7 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.

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.

When data type of one of the arguments does not match with the
type of the corresponding formal argument will results in some garbage.

c. 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.

When data type of one of the arguments in a prototype does not


match with the type of corresponding formal parameter in the header line,
compiler will produce an error.

d. 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.
When 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, the program will produce wrong output.
ANSI C REVIEW QUESTION SOLUTION

e. The type of expression used in return statement does not match with the type of the
function.
When the type of expression used in return statement does not match
with the type of the function, the return value is automatically cast to the
function’s type.

9.8 Which of the following prototype declarations are invalid?


Why?
(a) int (fun) void;
Answer:invalid ………… Correct answer: int fun ( void);

(b)double fun (void)


Answer:invalid ……….Correct answer: double fun (void); (Semicolon)

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


Answer: invalid……….Correct answer: float fun (float x,float y,float n);
(every variable should have datatype)

(d) void fun (void, void);


Answer: invalid………Correct answer: void fun (void);

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


Answer: invalid………Correct answer: int fun (int a, int b);
(every variable should have datatype )

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


Answer: valid

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


Answer: Invalid……..Correct answer: void fun (int a, int b);
Page 100

9.9 Which of the following header lines are invalid? Why?


Header line means DEFINITION.
At the case of definition semicolon must not be used.

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


Answer: invalid……… float average (float x,float y,float z)
(b) double power ( double a, int n-1)
Answer: invalid……… double power (double a, double n)
{Note that – expression can not be used in definition}

(c) int product ( int m, 10)


Answer:Invalid…… int product (int m, int 10)

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


Answer:Invalid………. double minimum (double x, double y)

(e) int mul ( int x , y)


Answer:Invalid…….. int mul (int x, int y)

(f) exchange ( int *a, int *b) [no error]


Answer:valid…………… exchange (int *a, int *b)
[Note: If there is no return type, it means as an integer to be returned ]

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


Answer:Invalid…………… void sum (int a, int b ,int c)
ANSI C REVIEW QUESTION SOLUTION

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


( a) void abc ( int a, int b)
{
int c;
............
return (c);
}
Answer: Error.
Return with a value , in function returning void (enabled by default )

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


{
...................
}

Answer: No Error
(c) int abc( int a, int b)
int abc( int a, int b)
{ {
double c = a+b; int c = a+b;
return (c); return (c);
} }
Answer : Error.
(d) void abc ( void)
{
..................
..................
return;
}

Answer : No error.
(e) int abc ( void)
{
..........................
...........................
return ;
}
Answer: No Error.
Page 102

9.11 Find errors in the following function calls:

On the occasion of FUNCTION CALL semi-colon(;) must be used

(a)void xyz();
Answer: Error.

(b)xyx (void);
Answer: Error

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


Answer: Error

(d)xyzz ();
Answer: no error.

(e)xyz ()+xyz();
Answer: no Error.

9.12 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


N.B: “ At the time of function call there no need to use “ data type” But here
in definition if there is no data type, it returns integer value “

(a) divide (10,2)


Answer: 5
(b) divide (9,2)
Answer: 4
ANSI C REVIEW QUESTION SOLUTION

(c) divide (4.5,1.5).


Answer: 3
(d) divide (2.0,3.0)
Answer: 0

9.13 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)
(a) divide (10,2)
Answer: 5
(b) divide (9,2)
Answer: 4
(c) divide (4.5,1.5).
Answer: 3
(d) divide (2.0,3.0)
Answer: 0

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


(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: 0.666666
Page 104

9.14: Determine the output of the following program?


int prod (int m, int n);
main ( )

{
int x=10; If there is “ z” the program
int y=20; will not run, but if there is
int p, q;
p=prod (x,y); “y” it will give the answer
,z
q=prod (p, prod (x )); & the output will be
printf (“%d %d\n”,p,q); 200 4000
}
int prod (int a,int b)
{
return (a*b);
}

Answer: 200 4000

9.15 What will be the output of the following program?


void test (int *a);
main( )
{
int x=50;
No semicolon after
test ( &x);
printf (“%d\n”, x); definition,
}
void test ( int *a) ; If there is a semicolon(;)
{ there will be error in output.
*a=*a +50;
}

Answer: 100.
ANSI C REVIEW QUESTION SOLUTION

9.15 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);
answer : x=3

int y= test (246 );


Answer : y=o

9.17 Enumerate the rules that apply to a function call.

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.
Page 106

9.18 Summarize the rules for passing parameters to functions


by Pointers.
1. The types of actual and formal argument must be same.

2. The actual arguments (in the function call) must be the addresses of
variables that are local to the calling function.

3. The formal arguments in the function header must be prefixed by the


indirection operator *.

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

5. To access the value of an actual argument in the called function, we


must use the corresponding formal argument prefixed with the indirection
operator *.

9.19 What are the rules that govern the passing of arrays to
functions.
1. The function must be called by passing only the name of the array.

2. In the function definition, the formal parameter must be an array type;


the size of the array does not need to be specified.

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

9.20 State the problems we are likely to encounter when we


pass global variables as parameters to functions
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 variable as parameters for passing value poses certain problems.

1. The values of global variables which are sent to the called function may
be changed inadvertently by the called function.

2. Functions are supposed to be independent and isolated modules. This


character is lost, if they use global variables.
ANSI C REVIEW QUESTION SOLUTION

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

4. A function that uses global variables suffers from reusability.

You might also like