Functions...hands-on...https://play.golang.org/p/XHEkOnEStFw...etc


(Cherolyn Lexvold) #1

I’m still around.
In this hands-on exercise, I was instructed to " Create a func which returns a func".
So I created this: https://play.golang.org/p/XHEkOnEStFw
So far so good.
Next, I was instructed to " assign the returned func to a variable".
Here is my attempt. https://play.golang.org/p/Z6tDOabooMx
When i formatted it, I got this message “prog.go:11:2: expected declaration, found sweettime”
Isn’t this a declaration? “sweettime := func()”
I don’t know which part of this topic should be surrounded by ```


(Cherolyn Lexvold) #2

I think I got it!
https://play.golang.org/p/KU8SJnk8QqI

Any comments?


(Jay Ts) #3

Hi Cherolyn

I’m happy to see you are still learning Go. :slight_smile:

Your final program looks good.

About your attempt that didn’t work, maybe you were trying to do something like this:

https://play.golang.org/p/BAxvdt-GZOl

or this

https://play.golang.org/p/o_LQnbZ0Yde

or this

https://play.golang.org/p/I7Bvkk4aqex

There are many ways to use functions. I just wanted to show you some variations. Actually, the way you did it is very common and many people prefer it.


(Cherolyn Lexvold) #4

Thank you very much! I like all three of your solutions!


(Cherolyn Lexvold) #5
fmt.Println(Hawaii()())

Can someone explain to me what all these parentheses are for? I think I know, but I would like it to be stated.


(Johan Dahl) #6

The function Hawaii is called by using Hawaii() and it returns a function. This returned function is called with the second pair of parentheses. The return of that function is then printed with fmt.Println(). One could have written it something like this instead

someFunction := Hawaii()
result := someFunction()
fmt.Println(result)

(Cherolyn Lexvold) #7

Your answer is interesting, informative, and helpful. Thanks


(Cherolyn Lexvold) #8

So…

func sweettime() int {
	fmt.Println("Amazing time")
	return 1981
}

Here sweettime both prints “Amazing time” and also returns an int? int is a return isn’t it? Yet it also can print something?


(BB-8) #9

Exactly! fmt.Println is going to print whatever you give it. Regardless of where you call it, whether it be in the body of a function or not.

func sweettime() int {
	fmt.Println("Amazing time")
	return 1981
}

Your sweettime function takes no parameters and returns an int. The return type is specified after the parentheses, but before the opening curly bracket.

If you were to remove return 1981 you would get a error, becuase by providing a return type you’re telling Go that it should expect an integer to be returned whenever sweettime is called.

You could alter your sweettime function to return a string of text as opposed to an integer like this:

func sweettime() string {
	fmt.Println("Amazing time")
	return "Sweet Time!"
}

and again, fmt.Println would still print "Amazing time" regardless, but this time rather than Go expecting an integer to be returned, it’ll expect a string which in the above case is "Sweet Time!".

An example of how you would use these returned values maybe a function that returns your name as a string.

func getMyName() string {
    return "Cherolyn Lexvold"
}

You could then define a variable equal to the returned value of calling that function. Like this:

func main() {
    myName := getMyName() // myName is now equal to "Cherolyn Lexvold"

   // now you have your name as a variable you can print it using fmt.Println
   fmt.Println(myName)
}

Here’s an example of the above that you can run: https://play.golang.org/p/DArotzSgz7j

The official Golang language tour has a section on Functions that you might find helpful https://tour.golang.org/basics/4

I hope this was helpful!