0

On Repeat Exercise

Hello guys, I'm so happy to learn on this platform. I've chosen Go to start with and I have a question for one of the exercises on the platform called "On Repeat". Can somebody more experienced by me tell where am I wrong: package main import "fmt" func main() { var w string fmt.Scanln(&w) var x int fmt.Scanln(&x) func repeat(w,x) { for i=1; i <=x; i++ { w*=x } repeat(w,x) } } I get the followint error: ./prog.go:11:7: syntax error: unexpected repeat, expecting ( Thanks in advance, Dido

19th Jun 2021, 7:39 AM
Dido Grigorov
Dido Grigorov - avatar
7 Antworten
+ 2
Dido Grigorov Your function definition is wrong. You have to define data type in function parameters. So It should be like this: func repeat(w string, x int) { for i := 1; i <= x; i++ { fmt.Println(w) } }
19th Jun 2021, 8:01 AM
A͢J
A͢J - avatar
+ 1
Dido Grigorov make repeat function outside the main function then call inside main function. You have to print w multiple times so don't multiply with x just print w
19th Jun 2021, 7:43 AM
A͢J
A͢J - avatar
+ 1
Dido Grigorov Instead of multiplying w with x just print w inside loop.
19th Jun 2021, 7:47 AM
A͢J
A͢J - avatar
+ 1
You are amazing man! Thanks a lot! :)
19th Jun 2021, 8:08 AM
Dido Grigorov
Dido Grigorov - avatar
0
Can you make a code example for me please?
19th Jun 2021, 7:45 AM
Dido Grigorov
Dido Grigorov - avatar
0
package main import "fmt" func repeat(w,x) { for i=1; i<=x; i++ { fmt.Println(w) } } func main() { var w string fmt.Scanln(&w) var x int fmt.Scanln(&x) repeat(w,x) } And now I get: # command-line-arguments /usercode/file0.go:4:13: undefined: w /usercode/file0.go:4:15: undefined: x /usercode/file0.go:6:6: undefined: i /usercode/file0.go:6:14: undefined: x /usercode/file0.go:6:17: undefined: i /usercode/file0.go:8:15: undefined: w
19th Jun 2021, 7:51 AM
Dido Grigorov
Dido Grigorov - avatar
0
Dido Grigorov If you want to solve problem using recursion then you can do like this: func repeat (w string, x int) { if x <= 0 { return } else { fmt.Println(w) x = x - 1 repeat (w, x) }
19th Jun 2021, 8:08 AM
A͢J
A͢J - avatar