Functions (Methods)

A function/method is a sequence of some declaration statements and executable statements that carry out a specific task.

Why do we use functions?
a) To cope with complexity.
b) Code reuse.
c) Hiding details

Methods live in classes.

A function prototype is the first line of the function definition that tells the program about the type of the value returned by the function and the number and type of arguments.

A function signature basically refers to the number and type of arguments.

A class method means that the method is not called through an instance of a class, but through the class itself. The static keyword is used to make class methods.

The void keyword indicates that a function doesn’t return any value. Only the functions returning a value can be used in expressions.

The parameters that appear in function definition are called formal parameters. The parameters that appear in function call are called actual parameters.

A function is invoked in two manners:
Call by value/Pass by value: It copies the values of actual parameters into the formal parameters.
Call by reference/Pass by reference: It passes a reference to the original variable of the actual parameters.

In Java, all primitive data types are passed by value and all the reference types are passed by reference.

The return keyword is used to terminate a function. It is also used to return a value to the caller of the function. A function can return only a single value.

There may be three types of functions in Java:
a) Computational functions: These functions calculate and return the computed value. Examples include Math.sqrt(), Math.pow().
b) Manipulative functions: These functions manipulate information and return success or failure status.
c) Procedural functions: These functions perform an action and have no explicit return values.

A pure function is one that takes objects or primitives as arguments but does not modify the objects. An impure function changes/modifies the state of the received objects.

A function having multiple definitions in the same scope that are differentiable by their function signatures, is called an overloaded function. This process is called function overloading.

Function overloading not only implements polymorphism, but also reduces the number of comparisons in a program, making the program run faster.

A member function with the same name as its class is called a constructor. It is used to initialize the objects of a class type with legal initial values.

Constructors have no return type. Not even void. Generally, a function should be defined as public so that its objects can be created in any function.

Differences between a constructor and a method:
a) A constructor creates an instance of a class. A function does a specific task.
b) A constructor has no return type. A function always has a return type.
c) A constructor is called during object’s creation. A function is called after object’s creation.

A constructor that receives parameters is known as a parameterized constructor. A constructor that doesn’t receive any parameters is known as non-parameterized constructor.

Non-parameterized constructors are also considered as default constructors. If a class has no explicit constructor defined, then the compiler will supply a default constructor.Declaring a constructor with arguments hides the default constructor.

It is even possible to overload constructors, by defining multiple constructors that differ only based on their signatures.

When a member function is called, it is automatically passed an implicit argument that is a reference to the object that invoked the function. That reference is called this.

The this keyword:
a) only pertains to instance methods.
b) is an implicit argument to the instance of the class.
c) is accessible inside any instance method.

An explicit call to a constructor allows us to create a temporary instance/temporary object. A temporary instance is one that lives in the memory as long as it is being used or referenced in an expression. They are deleted from memory when they are no longer referenced.

A function is said to be a recursive function if it calls itself.

A recursive definition is a definition that is made in terms of a smaller versions of itself.

Base case is a small problem that we know how to solve and it causes the recursion to end.

A recursive case is a more general case of the problem we’re trying to solve using a recursive function.

An infinite recursion occurs when a recursive function calls itself endlessly. It generally happens when the base case is missing.

Comparison between recursion and iteration:
a) When a loop repeats, it uses the same memory locations. But in recursion, fresh memory space is allocated each time.
b) Any problem that can be solved using iteration can also be solved using recursion, and vice versa.
c) Recursion is slower and uses more memory space when compared with loops.
d) In some cases, recursion is desirable because it makes the code easier to understand.

3 thoughts on “Functions (Methods)”

  1. Write a program in Java to accept a natural number N.Now display the twin prime numbers before and after N.
    Also display the nearest twin prime numbers from N.
    Eg:
    N=34
    Twin Prime numbers before 34 are:29,31
    Twin Prime numbers after 34 are:41,43
    Nearest twin prime numbers from 34 are:29,31

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: