Learn Programming

Learn D Programming Lesson 8 - Functions

What is a function?

A function is like a sub program that you can run from anywhere in a program. Functions allow you to not have to keep repeating the same lines of code all the time because you can put that code in a function and call the function a whole lot of times instead. The result of using functions is programs that use less code and are much easier to write.

How to write a function

We will start off by writing a very basic function that prints Hello on the screen. Here is the code for the function followed by an explanation of what it is made up of.

void PrintHello()
{
   writefln("Hello");
}

The first part of the function is the word void. This is called the return value which you will learn about in a moment. The next part says PrintHello() which is the function name. You can name your functions anything you like but it is a good idea to give them a name that tells you what they do. The function name is always followed by brackets which in this example don't contain anything but you will see what they are used for later. The code that belongs to a function must go between curly brackets. You should already know this because it is the same as with the main function which you use all the time.

Calling a function

For a function to run it needs to be called from somewhere. You call a function by using the name of the function followed by brackets and a semi-colon. When a function is finished running it returns to the place where it was called from. Here is an example that calls the PrintHello function 3 times from the main function and prints Hello 3 times. PrintHello is called 3 times to show you how a function can be called as many times as you like.

import std.stdio;

void main()
{
   PrintHello();
   PrintHello();
   PrintHello();
}

void PrintHello()
{
   writefln("Hello");
}

Local and global variables

You can declare a variable either inside a function or outside all functions. If it is declared inside a function then it is called a local variable and can be used by only that function. A global variable is declared outside of all functions and can be used by any function. Here is an example of a local variable being used inside a function.

import std.stdio;

void main()
{
   MyFunction();
}

void MyFunction()
{
   int MyVariable; // Local variable
   MyVariable = 5;
   writefln(MyVariable);
}

Here is an example of using a global variable. The variable is given a value in the main function and its value is changed in another function before being printed again in the main function.

import std.stdio;

int MyVariable; // Global variable

void main()
{
   MyVariable = 5;
   ChangeValue();
   writefln(MyVariable);
}

void ChangeValue()
{
   MyVariable = 7;
}

Parameters

A function can have values passed to it by having parameters. A parameter is just like a variable but you declare them in the brackets after the function name. When you add a paramter to a function you must first specify the data type and then give the parameter a name. When you call a function that has parameters you must provide values for each of the parameters. To pass a value to a function that requires a parameter all you have to do is put the value inside the brackets that follow it when calling the function. Here is an example of a function called PrintNum that has an integer called MyNum as a parameter and prints the value of MyNum on the screen. The function is called twice with 2 different values to show you how different values can be passed to a function.

import std.stdio;

void main()
{
   PrintNum(5);
   PrintNum(7);
}

void PrintNum(int MyNum)
{
   writefln(MyNum);
}

A function can have more than one parameter but you must separate them with a comma. Here is an example that has a function that has 2 parameters and prints them both.

import std.stdio;

void main()
{
   Print2Nums(6, 8);
}

void Print2Nums(int num1, int num2)
{
   writefln(num1);
   writefln(num2);
}

Return values

In the example above the word void is always put before a function name. This is the return value. void is a special type which means don't return anything. You can use the return value to return a value of any data type. For example you can return an integer by changing void to int or you can return a string by changing void to string. The return value lets you return a value to the place where the function is being called from. When a function has a return value you must use the return keyword to return the value at the end of the function. Here is an example of a function called Add which takes 2 integers as parameters and adds them together and then returns a value which is stored in a variable called result.

import std.stdio;

void main()
{
   int result = Add(3, 4);
   writefln(result);
}

int Add(int num1, int num2)
{
   int NumbersAdded = num1 + num2;
   return NumbersAdded;
}

Reference parameters

When you pass a variable to a function it makes a copy of the value of the variable and if you change the value of the parameter in the called function then no changes are made to the original variable that was passed to it. If you want to change the value of a variable passed to a function then you must declare the parameter as being a reference parameter by using the ref keyword in front of the parameter. A good example of when you would want to use a reference parameter is when you want a function to return more than one value because a function can only return one value but you can have as many reference parameters as you like. Here is an example that has has an Add method that does the same thing as in the example higher up but instead of returning the value it uses a reference parameter to change a variable that has been passed to it.

import std.stdio;

void main()
{
   int result;
   Add(3, 4, result);
   writefln(result);
}

void Add(int num1, int num2, ref int NumbersAdded)
{
   NumbersAdded = num1 + num2;
}

Function overloading

Usually you can't create functions that have the same name. If you want a function to have the same name as another function then you have to make sure that they both have different parameters. This is called function overloading. You can overload a function by either using different data types for the parameters or by having a different number of parameters. Here is an example that has an Add method that adds 2 numbers and also has an Add method that adds 3 numbers and also has an Add method that joins 2 strings together.

import std.stdio;

void main()
{
   int result = Add(3, 4);
   writefln(result);
   result = Add(3, 4, 5);
   writefln(result);
   string strresult = Add("Part1", "Part2");
   writefln(strresult);
}

int Add(int num1, int num2)
{
   int NumbersAdded = num1 + num2;
   return NumbersAdded;
}

int Add(int num1, int num2, int num3)
{
   int NumbersAdded = num1 + num2 + num3;
   return NumbersAdded;
}

string Add(string s1, string s2)
{
   string StringsAdded = s1 ~ s2;
   return StringsAdded;
}

Default parameters

A default parameter is a parameter whose value is set when it is declared and when you call the function you can either choose to pass a value for that parameter or you can choose to not pass the value in which case the default value will be used. In other words you can use default parameters to make the parameters of a function optional. Here is an example of a function that prints a word as many times as is specified by the second parameter but if no second parameter is passed then it only prints it once.

import std.stdio;

void main()
{
   Print("Print this 5 times", 5);
   Print("Print this once");
}

void Print(string word, int times = 1)
{
   for(int i = 1; i <= times; i++)
      writefln(word);
}

Variadic functions

A variadic function is one that lets you pass as many parameters to it as you like which means you can pass either one parameter or you can pass 5 or 10 or as many as you like. There are a few different ways to handle variadic function parameters but the easiest to understand is by using an array as the parameter followed by 3 dots. Here is an example that has a function called PrintNums which has a variadic function parameter called nums which is an array of ints.

import std.stdio;

void main()
{
   PrintNums(3, 4);
   PrintNums(5, 6, 7);
}

void PrintNums(int[] nums ...)
{
   for(int i = 0; i < nums.length; i++)
      writefln(nums[i]);
}

Built-in functions

The D programming language has a whole lot of built-in functions that you can use in your programs. One example that we have been using already is the writefln function. It is not possible to look at all of them in just one lesson but you can have a look at the built-in functions for D and see what is possible. It is hard to understand how to use them in the beginning but as you become a better programmer it will become much easier.

Practice

Write a function that has 2 parameters. The first one is a number and the second is the number of times the first number must be added to itself. So for example if the first paramater is 5 and the second parameter is 3 then it must return 15.