At this point, you may be curious what func main() exactly is. In programming, this is called a function definition, and it is used to create blocks of re-usable code we can call anywhere.
But what does "calling" a function mean? Well it's simple. when we type main(), we are telling GLang to run the function main we defined. It's like calling your friends and saying "Let's hang out!"
Functions can have other names then main, for example, we can make a function called eat_breakfast:
func eat_breakfast() {
bark("I am eating breakfast!");
}
But do be warned, function names cannot have spaces:
func eat breakfast() { # this line causes an error
bark("I am eating breakfast!");
}
The parenthesis next to a function's name define the arguments of a function. Arguments are objects you can give the function to use internally. Take a look at this greet function:
func greet(name) {
bark("Hello, " + name);
}
name is an object you can give to the function when calling it:
greet("George");
Arguments are positional, meaning we can't give a function expecting one argument ten arguments. If a function expects x amount of arguments, we must give it all required arguments:
greet("George", "John"); # this causes a 'positional argument error'
greet(); # this will also cause a 'positional argument error'
A great example of a proper function is the bark function we have been using. This is called a built in function and is essentially included within the language (you can see all the built in functions here.)
Currently, we are calling the bark function with its single argument included:
bark("this is an argument of a function");