Learn to Code via Tutorials on Repl.it!

← Back to all posts
Golang Google's C++ Part 4: Functions!
JustAWalrus (1187)

Firstly, thank you guys for the support just don't inform @HahaYes that we might pass him sooner rather than later!

Secondly, I have set a goal to post 5 of these a day. Once we finish Go we might do Java or something but that'd be in a couple weeks.

Oh and another thing code that starts with // is a comment!

Okay so you guys have learned a lot hopefully by now. And today isn't going to be different.

We are talking about functions!

What's a function?!?!?

Well, good friend, a function is a fundamental in programming. Its basically a block of code that can be called multiple times and can be passed arguments and return something!

EXAMPLE

func returnHi(name) (x){
var x string = "Hi " + name
return x
}

So let's break this down:

func is a keyword used to tell Go that you are making a function.

returnHi is the name (this can be anything as long as it is not a keyword).

(name) Inside the first pair of parenthesis we put the arguments and whenever the function is called we do this:

<name>(<args>)

So the arguments are passed when the function is called and your function stores them in a variable that is ONLY accessible to the function scope.

The (x) is where we define the variable we are going to be returning.

The return is where we return that variable.

But how does returning work?!??!?

Well whenever the person calls the function they can set a variable equal to the return value.

Note that if the function returns multiple items these will be in the order specified!

But how do we take in multiple return values?

EXAMPLE:

x, y := somefunction()

We separate the names with commas!

But what if we don't want to take in all the return values?

Well We can do something like this:

_, y := somefunction()

We put a _ as a placeholder!

Anyway that is it for now don't forget to upvote!

Part 5: https://repl.it/talk/learn/Golang-Googles-C-Part-5-For-Loops/43033

Comments
hotnewtop
Viper2211 (86)

This is awesome!!!! I've been wanting to learn go for so long, and finally, a tutorial I can understand!

JustAWalrus (1187)

Thank you, just upvote and share with friends :D @Viper2211

HahaYes (1862)

btw you literally pinged me

awesome10 (231)

HOW IS THIS NOT ON TOP

awesome10 (231)

@awesome10 im so sorry hahayes but i do like google quite a lot... and this is google... :/

JustAWalrus (1187)

yes, you can even look it up :D @awesome10

awesome10 (231)

oh i know what im going to do: im going to use functions to basically make everything like python (shh dont tell)

JustAWalrus (1187)

if you like python, i have a repl i wanna do but ave no real reason to we could do that together. :D @awesome10

awesome10 (231)

@Wuru hmm... ok, what is it about? (hmm doesnt mean anything, its put there for no reason at all like putting comments in code that no one will ever read)(i totally dont do that)

JustAWalrus (1187)

its an os(not really bootable just an emulator) If you do the hardcoding it wont be too hard. :P @awesome10

JustAWalrus (1187)

man, stop. I need a close following on my tutorials not a few comments xD @awesome10

awesome10 (231)

@Wuru comments mean it'll be on the top, meaning people will see it, meaning they'll upvote, meaning you have followers!

awesome10 (231)

@Wuru are there too many meanings?

JustAWalrus (1187)

no, there are never enough meanings >:D @awesome10

awesome10 (231)

come on this comment should do it