5.1. Introducing methods#

You have been using methods for quite a while now. All those times where you’ve created the main method and have een calling the print method to output results from the terminal.

You will write code for this section in a different directory to the one used for the Hello World program. Follow the following instructions below to do so:

  1. Create a new directory called Methods under your ValaProjects directory.

  2. Under the Methods directory you have just created, create a new file called main.vala.

5.1.1. Methods in more detail#

As explained previously in the Hello World section, a method is a block of code that contain code for the program to execute when called.

Note

These are technically functions because they aren’t part of an instance of a class but in Vala we call functions “methods” anyway.

Now let’s create a program that makes use of a method that you create and call.

First, let’s start with a Hello World program

main.vala#
public static void main () {
    print ("Hello World\n");
}

Now let’s create a method to move the printing logic outside of the main method.

public static void say_something () {
    print ("Hello World\n");
}

public static void main () {
    say_something ();
}

The output of this program will still be:

Hello World

5.1.2. Updating our new method#

One of the benefits of using methods is that we are able to change what happens when the a method is called, without having the change the initial method call line.

To demonstrate this, in say_something, change the text that gets output into something of your choice.

In the following example we’ll be using the text “The quick brown fox jumped over the lazy dog”:

public static void say_something () {
    print ("The quick brown fox jumped over the lazy dog\n");
}

public static void main () {
    say_something ();
}

If you compile and run the program the program above, it will have the following output:

The quick brown fox jumped over the lazy dog

5.1.3. Adding Method Parameters#

We can also pass in data to the method call, to influence the output generated by a method:

Update say_something () so that it takes in a string with the name of name, and prepend (the value of name) “says:” to the print method call.

public static void say_something (string name) {
    print (@"$name says: The quick brown fox jumped over the lazy dog\n");
}

// More code below

Now you will have to pass in a name to the say_somthing () method call in the main method.

public static void say_something (string name) {
    print (@"$name says: The quick brown fox jumped over the lazy dog\n");
}

public static void main () {
    say_something ("Julie");
}

Now if you compile and run the code above, it will output:

Julie says: The quick brown fox jumped over the lazy dog

5.1.4. Returning data from methods#

Methods can also return data.

In main.vala add a method called double_number that takes in and integer and returns an integer:

public static void say_something (string name) {
    print (@"$name says: The quick brown fox jumped over the lazy dog\n");
}

public static int double_number (int number) {

}

public static void main () {
    say_something ("Julie");
}

Now inside of the double_number method, return the result of number multiplied by 2:

public static void say_something (string name) {
    print (@"$name says: The quick brown fox jumped over the lazy dog\n");
}

public static int double_number (int number) {
    return number * 2;
}

public static void main () {
    say_something ("Julie");
}

Lastly, let’s test the double_number method. We’ll double the number 2 and print out the answer, which should be 4:

public static void say_something (string name) {
    print (@"$name says: The quick brown fox jumped over the lazy dog\n");
}

public static int double_number (int number) {
    return number * 2;
}

public static void main () {
    say_something ("Julie");
    print ("If we double 2, we get: %d", double_number (2));
}

Now if you compile and run the program above, we should see the following output:

Julie says: The quick brown fox jumped over the lazy dog
If we double 2, we get: 4