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

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 ```

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

Any comments?

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.

2 Likes

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

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.

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)
3 Likes

Your answer is interesting, informative, and helpful. Thanks

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?

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: Go Playground - The Go Programming Language

The official Golang language tour has a section on Functions that you might find helpful A Tour of Go

I hope this was helpful!

Like this? https://play.golang.org/p/ytvZCFR9z1b

Not only was your reply helpful, it was also thoroughly enjoyable!

fmt.Println is a very cool feature!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.