Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

FUNCTIONS

1.Define a function.What is function prototype?

Ans:A function is a named code block.The function prototype is


the first line of the function definition.It tells the program about
the return data type and the names and data types of the
parameters to the function.

2.What are actual and formal parameters?

Ans:The parameters listed in the function prototype rae termed


as the formal parameters whereas the parameters used in the
function call statement are the actual parameters.

3.How many values can be returned from a function?

Ans:A function can return at the most one value.

4.What is the condition of using a function in an expression?

Ans:If a function is to be used in an expression,it must return a


value.

5.What is the principle reason for passing arguments by values?

Ans:Parameters should be passed by value if the programmer


wants that the called program should not be able to alter the
values of the actual parameters.

6.What is the principle reason for passing arguments by


reference?In a function call,what data items can be passed by
reference?

Ans:The parameters/arguments should be passed by reference if


the changes made by the called program to the formal
parameters are to be reflected back in the corresponding actual
parameters,after the called program terminates.in java,only
objects,arrays and strings can be passed by reference.

7.What is the role of a return statement in a function?

Ans:The return statement can be used in a function to return the


control as well as the value to the calling function.

8.Write the advantages of useing functions in programs.

Ans:a)Functions lessen the complexity of programs.


b)Functions hide the implementation details.
c)Functions enhance the reusability of code.

9.What is polymorphism?How does function overloading


implement polymorphism?

Ans:The ability of an entity to exhibit different behavior


depending upon the context in which it is used is termed as
polymorphism.By means of function overloading,the same
function name can be associated with different
definitions.Depending upon the parameters used to call the
function,the decision regarding the function to be called at
runtime is made by the compiler automatically.

10.What is function overloading?

Ans:Function overloading is the act of defining multiple functions


with the same name and different argument lists.Two overloaded
functions cannot differ just in the return type.

11.What is the significance of function overloading in java?

Ans:Function overloading helps in avoiding the use of if-else


statements to decide which functions should be called in which
context.As in case of overloaded functions,the compiler
automatically takes this decision based upon the parameters
used to call the function.

12.The string objects being referenced by type are passed by


reference,but changes,if any,are not reflected back to them.Why?

Ans:The string objects are immutable in java which means once


they are created,they cannot be changed.That is why,even
though strings are passed by reference,they cannot be changed.

13.What is the role of the void keyword in declaring functions?

Ans:There are times when a function is not required to return a


value,but the java language requires all functions except
constructors to specify a return data type.In such a case,the void
keyword is used to avoid the compile time error.This keyword,if
used as the return data type,indicates that the function does not
return any value.

14.Why do you think function overloading must be a part of an


object oriented language?

Ans:Function Overloading must be a part of an object oriented


language as it is this feature that implements polymorphism in an
object oriented language.That is the ability of an object to
behave differently in different circumstances which can
affectively be implemented in programming through function
overloading.
Also with function overloading,the programmer is relieved from
the burden of choosing the right function for a given set fo
values.This important reponsibility is carried out by the compiler
when a function is overloaded.

You might also like