Creating (defining) a function

It’s time to learn how to create your own functions! The code in which the function is created is called the function definition.

Here is the simplest example of a function that prints the “Hello world!” to the console (recall, this is a side effect):

static void sayHello() {
  System.out.print("Hello world!");
}

Methods may or may not return values, may be called with or without parameters. The type of data returned is specified when a method is declared - before its name.

In the example above, the method simply prints to the console and should not return anything, so the return type is void.

In the method header, first come modifiers that determine under what conditions it is available for a call.

In the method header, first go modifiers that determine under what conditions it is available for a call. static means that a function does not use members of the class to which it belongs (recall that in Java every function belongs to some class, but we do not study classes in this course).

In order for a Java application to work, in some of its classes it is necessary to declare such a main function, as we did earlier in all examples:

public static void main(String [] args) {
}

It is this function that will be called when the application is started, so we wrote the code in it that we wanted to execute. In the examples, we declared this function in the App class.

Let’s create our first function. She will have one task: to display the text Today is: December 5.

It is necessary to name the function so that its name is clear from the title. Let’s give it the name showDate:

static void showDate() {
  var text = "Today is: December 5";
  System.out.print(text);
}

There are only two lines of code in our function, but there can be as many of them as you like. Functions can be considered programs within programs.

Here is the complete program, with function definition and call:

public class App {
  public static void main(String[] args) {
    App.showDate();
  }

  static void showDate() {
    var text = "Today is: December 5";
    System.out.print(text);
  }
}

Not only the main function, but also showDate is described in the App class, so the showDate function is called by the App.showDate expression - but in fact if both functions are described in the same class as here, then one of them can call the other without class name indication: not App.showDate(), but just showDate().

Today is: December 5

We could use a similar function on the website or in any program to display the current date on the screen. But in its current form, the function will not cope with the task, because it always displays Today is: December 5.

Let’s improve the function so that it takes a date as an argument and displays it on the screen.

Define and call our updated function:

public static void main(String[] args) {
  showDate("January 29");
}

public static void showDate(String date) {
  String text = "Today is: December " + date;
  System.out.print(text);
}
Today is: January 29

Now it is clear why we needed parentheses after the function name: you can specify the arguments in. Now our function takes one argument - text, which is of type String (this is the type in Java that matches the string of text).

Note: we do not create the text variable, but use it in the function body. Java works like this: the variable itself is created when the call is made and the specified value (in our example, 'January 29') is written to this variable.

Arguments can be called arbitrarily, their names make sense exclusively in the function body.


Arguments may be several. In this case, we do the same thing in the function definition as in the call: we simply specify the arguments separated by commas.

A complete example of defining a function with multiple arguments and calling it:

public static void main(String [] args) {
  showDate ("January", "29");
}

static void showDate(String month, String day) {
  var text = "Today is: December" + month + "" + day;
  System.out.print (text);
}
Today is: January 29

The main thing is to remember about the order: in what order the arguments are in the definition of the function, in the same order they must be transmitted when calling.

instructions

Implement the printJaimesLine function, which takes one argument, a string, and displays a replica on the screen in the format JAIME: passed_string.

How to name a variable that will be an argument - decide for yourself.

Our system contains code hidden from you. In this exercise, the call to the printJaimesLine function is hidden. So we check your decision.

You do not need to call the function yourself, just define it. But for clarity, this is how our system calls it:

App.printJaimesLine("Farewell, my friend ...");

so that we can call this function from another class, we need to mark it not only with the keyword static, but also public.

JAIME: Farewell, my friend ...

If you got stuck and don't know what to do, you can ask a question in our huge and friendly community
Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on GitHub.