Functions in java with examples

Functions in java

The goal of every programmer is to save time in both programming and debugging. However, there comes a time when you write hundreds of lines of code. It’s very frustrating having to repeat the same code now and then. It is more frustrating when your code gets some error, and you will need to debug the whole program.

Improve your programming expertise by using functions. Functions help in avoiding ambiguity. You will agree that breaking down the code into smaller chunks in functions will help you organize your work, and debugging becomes easy. Why repeat the same lines of code several places where you can put it inside a function and call it whenever you need to perform that task. Code reusability saves a lot of time.

In this tutorial, we will spare some time and discuss functions. In java, a method is the same as a function. All the functions must be defined within a class. By that, we can summarize by defining a Java method as a function belonging to a class. A function is a named unit of code that can be invoked anywhere in the class.

Syntax of a function

Public static void myFuction(String name, int age ) < // fuction code >
  • Access specifier – this shows the scope of availability of a fuction. ‘Public’ means that the function can be called from aywhere in the program. We have other access specifiers such as private and protected.Protected, the method can only be called within the class and its subslcasses. Private can oly be called inside the class
  • Modifier – ‘static’ is optional in a fuction defination. In this case static means the function is a not an object of the main class but a method that belongs to the main class.
  • Retur type – We have functions that return a value and functions that do not return anything. Void, means that the function does not have a return value. If the fuction was to return a value, replace void with the data type of the returned value.
  • Function name – This the name of the function
  • Parameter list – it informs the compiler about the data type it will receive and the value to be returned.
Читайте также:  Java api или javascript api

Advantages of functions in Java

  1. Reusability of code – functions help in removing repetition of code in a program. With functions you can define it once and call it anywhere in the program to perform the task you need and you are not limited to the number of times you call the function. Lastly it’s simple to relace the functions into libraries. This allows them to be used by more than one program.

2. Divide and conquer – Using functions helps break a program into smaller manageable chunks, hence making debugging and testing easier. Functions help in collaboration by breaking up the work into tasks for team development.

Источник

Functional Programming in Java with Examples

So far Java was supporting the imperative style of programming and object-oriented style of programming. The next big thing what java has been added is that Java has started supporting the functional style of programming with its Java 8 release. In this article, we will discuss functional programming in Java 8.
What is functional programming?
It is a declarative style of programming rather than imperative. The basic objective of this style of programming is to make code more concise, less complex, more predictable, and easier to test compared to the legacy style of coding. Functional programming deals with certain key concepts such as pure function, immutable state, assignment-less programming etc.
Functional programming vs Purely Functional programming:
Pure functional programming languages don’t allow any mutability in its nature whereas a functional style language provides higher-order functions but often permits mutability at the risk of we failing to do the right things, which put a burden on us rather than protecting us. So, in general, we can say if a language provides higher-order function it is functional style language, and if a language goes to the extent of limiting mutability in addition to higher-order function then it becomes purely functional language. Java is a functional style language and the language like Haskell is a purely functional programming language.
Let’s understand a few concepts in functional programming:

  • Higher-order functions: In functional programming, functions are to be considered as first-class citizens. That is, so far in the legacy style of coding, we can do below stuff with objects.
    1. We can pass objects to a function.
    2. We can create objects within function.
    3. We can return objects from a function.
    4. We can pass a function to a function.
    5. We can create a function within function.
    6. We can return a function from a function.
  • Pure functions: A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something.
  • Lambda expressions: A Lambda expression is an anonymous method that has mutability at very minimum and it has only a parameter list and a body. The return type is always inferred based on the context. Also, make a note, Lambda expressions work in parallel with the functional interface. The syntax of a lambda expression is:
  • In its simple form, a lambda could be represented as a comma-separated list of parameters, the –> symbol and the body.
Читайте также:  Import css file in html

How to Implement Functional Programming in Java?

Источник

Оцените статью