User Defined Functions - Review Question Solution
User Defined Functions - Review Question Solution
User-Defined Functions
(c) A function can be defined and placed before 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.
(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.
(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.
(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
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.
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.
………
void main ()
int ans;
}
[Note: parameter and argument is same thing]
Used only one name in a program. Same variable names are working
different values in different
ANSI C REVIEW QUESTION SOLUTION
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.
Scope:
Visibility:
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.
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.
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
(a)void xyz();
Answer: Error.
(b)xyx (void);
Answer: Error
(d)xyzz ();
Answer: no error.
(e)xyz ()+xyz();
Answer: no Error.
return (x/y);
}
{
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: 100.
ANSI C REVIEW QUESTION SOLUTION
What will be the values of x and y when the following statement are executed
int x = test (135);
answer : x=3
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
2. The actual arguments (in the function call) must be the addresses of
variables that are local to the calling function.
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.
1. The values of global variables which are sent to the called function may
be changed inadvertently by the called function.
3. It is not immediately apparent to the reader which values are being sent
to the called function.