Skip to content
Sign upLog in
← Back to Community

Golang Google's C++ Part 4: Functions!

Profile icon
JustAWalrus

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

Voters
Profile icon
TusharRawat
Profile icon
Wuru
Profile icon
zararitz
Profile icon
rileyhead
Profile icon
SarahHeisnoran
Profile icon
ARJPEG
Profile icon
Viper2211
Profile icon
mkw
Profile icon
awesome10
Profile icon
JustAWalrus
Comments
hotnewtop
Profile icon
JustAWalrus

TOPPPPP

Profile icon
Viper2211

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

Profile icon
JustAWalrus

Thank you, just upvote and share with friends :D

@Viper2211

Profile icon
HahaYes

btw you literally pinged me

Profile icon
HahaYes

Hello. I have the monopoly here.

Profile icon
DynamicSquid

@HahaYes
business!

Profile icon
JustAWalrus

haha

@DynamicSquid

Profile icon
awesome10

HOW IS THIS NOT ON TOP

Profile icon
awesome10

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

Profile icon
JustAWalrus

yes, you can even look it up :D

@awesome10

Profile icon
awesome10

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

Profile icon
JustAWalrus

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

@awesome10

Profile icon
awesome10

@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)

Profile icon
JustAWalrus

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

@awesome10

Profile icon
JustAWalrus

want me to invite?

@awesome10

Profile icon
awesome10

@Wuru
yeah sure!

Profile icon
JustAWalrus

sent invite

@awesome10

Profile icon
awesome10

argh so close

Profile icon
JustAWalrus

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

@awesome10

Profile icon
awesome10

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

Profile icon
awesome10

@Wuru
are there too many meanings?

Profile icon
JustAWalrus

oh yea lol

@awesome10

Profile icon
JustAWalrus

no, there are never enough meanings >:D

@awesome10

Profile icon
awesome10

@Wuru
xD

Profile icon
awesome10

oh no not yet

Profile icon
awesome10

come on this comment should do it