I'm going to write the understanding of Go from the basics of programming!
var input string
fmt.Println("Please enter the following words:go")
fmt.Scan(&input)
fmt.Printf("%s was entered", input)
A function is "a collection of several processes" If you put the processing into a function, you can use it as many times as you like.
Put the above code into an ask function
func ask() {
var input string
fmt.Println("Please enter the following words:go")
fmt.Scan(&input)
fmt.Printf("%s was entered", input)
}
The function is written as "func function name ()" You can prepare a function by writing the process you want to summarize in "{}"
func main() { //call the ask function
ask()
}
func ask() {
var input string
fmt.Println("Please enter the following words:go")
fmt.Scan(&input)
fmt.Printf("%s was entered", input)
}
Just defining a function does not execute the processing in it You can execute the processing in the function by writing "function name ()"
func main() {
ask()
ask() //Can be called multiple times
}
func ask() {
var input string
fmt.Println("Please enter the following words:Go")
fmt.Scan(&input)
fmt.Printf("%s was entered", input)
}
//console
//Please enter the following words:go
//go(Type in console)
//Entered go
//Please enter the following words:go
//go(Type in console)
//Entered go
The advantage of the function is that once it is defined, the same process can be executed many times.
func main() {
ask("go")
}
You can pass a value by writing a value in "()" when calling a function. This value is called an argument
func main() {
ask("go") //Passing Go as an argument to question
}
func ask(question string) { //question → variable name
...
In order to use the passed value, it is necessary to prepare a variable to receive The string "cat" is received by the string type variable question
func main() {
ask("go") //Passing go as an argument to question
ask("ruby") //Passing ruby as an argument to question
}
func ask(question string) {
...
fmt.Printf("Please enter the following words: %s\n", question)
//console
//Please enter the following words:go
...
//Please enter the following words:ruby
...
The passed value can be used with the received variable name You can change the problem statement by changing the value passed
func main() {
text := "Go"
ask(text)
func ask(question string) {
...
fmt.Printf("Please enter the following words: %s\n", question)
}
//console
//Please enter the following words:go
...
You can also pass the value of a variable as an argument to the function Passing the variable text as an argument to the variable question of the ask function
func main() {
ask(1,"go")
ask(2,"ruby")
ask(3,"python")
}
func ask(number int, question string) {
fmt.Printf("[Question%d]Please enter the following words: %s\n", number, question)
}
//console
//[question 1]Please enter the following words:go
//[Question 2]Please enter the following words:ruby
//[Question 3]Please enter the following words:python
When using multiple arguments, pay attention to the order in which they are passed The order of the arguments of the ask function is (problem number, problem sentence)
func ask(number int, question string) int {
fmt.Printf("[Question%d]Please enter the following words: %s\n", number, question)
...
return 10 //Return value of ask function
A value can be returned to the caller by using the return value (return).
The ask function receives "1" and "go" and returns the result "10" to the caller. This "10" is the return value The int on the first line is the return type
func main() {
totalScore := ask(1, "go") //The return value of the ask function is 10
fmt.Println("Score", totalScore)
}
func ask(number int, question string)int {
...
return 10
//console
//[question 1]Please enter the following words:go
//Score 10
The return value of the function can be received by assigning it to a variable, etc. Call the ask function and assign the return value 10 returned to the variable totalScore.
func main() {
totalScore := ask(1, "go")
totalScore += ask(2, "ruby")
totalScore += ask(3, "python")
fmt.Println("Score", totalScore)
}
func ask(number int, question string)int {
...
return 10
By using "+ =", the return value of the ask function can be added as it is.
func ask(number int, question string)int {
fmt.Scan(&input)
if question == input {
fmt.Println("Correct answer")
}else{
fmt.Prantln("Incorrect answer")
}
}
Conditional branch to judge correct or incorrect answer Judgment by whether the question sentence given and the entered value are the same
func main() {
totalScore := ask(1,"go")
totalScore += ask(2,"ruby")
totalScore += ask(3,"python")
fmt.println("Score",totalScore)
}
func ask(number int, question string)int {
if question == input {
fmt.Println("Correct answer")
return 10
}else{
fmt.Println("Incorrect answer")
return 0
}
If the answer is correct, return 10 If the answer is incorrect, return 0 Add the returned value to the totalScore of the main function
func main() {
totalScore := 0
ask(1,"go")
ask(2,"ruby")
ask(3,"python")
fmt.Println("Score",totalScore)
}
func ask(number int, question string) {
if question == input {
fmt.Println("Correct answer")
totalScore += 10 //Error when trying to add directly to totalScore of main function from within ask function
}
The variable totalScore defined in the main function is used without using the return value. I can't even try to use it in the ask function
func main() {
totalScore := 0
//The range in which the variable totalScore can be used(In scope)
}
func ask(number int, question string) {
//Range where the variable totalScore cannot be used(Out of scope)
}
Because the variable totalScore defined in the main function can only be used in the main function Variables have a usable range, and that range is called the scope of the variable.
Scope is very important
func main() {
totalScore := 0
ask(1,"go",totalScore)
...
fmt.Println("Score",totalScore)
}
...
func ask(number int, question string, totalScore int) {
...
if question == input {
fmt.Println("Correct answer")
totalScore += 10
...
}
}
No error occurs, but the score is not added even if the answer is correct
func main() {
totalScore := 0
//Scope of variable totalScore of main function
}
func ask(number int, question string, totalScore int) {
totalScore += 10
//Ask function argument totalScore scope
}
With totalScore used in the main function Because the totalScore used in the ask function has a different scope Same name but different variable
It looks like you're passing a variable from the main function to the ask function Actually, the value of the variable totalScore is copied and passed to the ask function. Therefore, even if you add a value to the totalScore of the ask function, the original value does not change.
Recommended Posts