Accessing Functions

Methods or functions are the main building blocks of Java programs. They can be called several times in the same program, allowing the code to be reused. A Java method is a collection of statements that is grouped together to perform an operation. A function exists in three different forms within a program: method prototype, method definition and method call. A function is accessed by providing the method name along with the argument list enclosed in the parenthesis. A parameter is a value that is passed to a method. The values passed to the method during its call from the caller are called arguments. Arguments to a function can be primitive data types and reference data types.

There are two types of parameters - actual parameters and formal parameters. The parameters that appear in function call statement are called actual parameters. The parameters that appear in method definition are called formal parameters. There are two ways for passing argument to the method - call by value and call by reference. In call by value, in place of passing a reference, a copy of the actual argument is passed to the method. In call by reference, in place of passing a value, a reference to the original variable is passed to the method. Arguments passed to main() method are known as command line arguments.

To Access the full content, Please Purchase

  • Q1

    What is the role of return-type in function call?

    Marks:1
    Answer:

    In the call statement, a variable that accepts the return value from a function should be as per the return type of the function.

    View Answer
  • Q2

    How is the function given below called?
    void addNum(int argI, double argD);

    Marks:1
    Answer:

    code to call the given function:
    int a = 10;
    double b = 34.87;
    addNum(a, b); //function called

    View Answer
  • Q3

    How many ways values are passed to the function?

    Marks:1
    Answer:

    Values are passed to the functions in two ways:
    1.Pass by value
    2.Pass by reference

    View Answer
  • Q4

    Define the term ‘function arguments’.

    Marks:1
    Answer:

    Function arguments are the values of variables that are passed to the function during a function call.

    View Answer
  • Q5

    How is a function called?

    Marks:1
    Answer:

    A function is called (invoked or executed) by providing the function name, followed by the parameters.

    View Answer