Go Language-Functions

function

I'm going to write the understanding of Go from the basics of programming!

What is a function

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

Function definition

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 "{}"

Function call

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 ()"

Call the function multiple times

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.

Pass a value to a function

func main() { 
  ask("go")  
}

You can pass a value by writing a value in "()" when calling a function. This value is called an argument

Receive arguments

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

Use arguments

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

Pass the value of the variable

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

How to pass multiple arguments

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)

What is the return value?

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

How to receive the return value

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.

Calculate return value

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.

Branch of correct and incorrect answers

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

Returns a return value

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

Things to keep in mind when passing values ​​to functions

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

Variable scope

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

Pass the score as an argument

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

Variable with the same name

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

Copy of value

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

Go Language-Functions
Go Language-Basic ❶
Go language-fmt.Scan
Go language-address
Go language-pointer
Go Language-Basic ❷
GO Chokisomemo 1
Go language-iteration
Python with Go
Go language-conditional branching
About Go functions
Go language-standard package
Go class basics
Go language-rand package
About Go Interface
use go module