> For the complete documentation index, see [llms.txt](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/golang/tipe-data-reference/function-in-golang/function-sebagai-parameter.md).

# Function sebagai Parameter

Function bisa digunakan sebagai parameter untuk function yang lain.

## Contoh *code* 1

```go
package main

import (
	"fmt"
)

// type declaration
type Filter func(name string) string

// function as parameter
func sayHelloWithFilter(name string, filter Filter) {
	nameFiltered := filter(name)
	fmt.Println("Hello", nameFiltered)
}

func spamFilter(name string) string {
	if name == "Kucing" {
		return ""
	} else {
		return name
	}
}
	
func main() {
	filter := spamFilter
	sayHelloWithFilter("Alan", filter)
	sayHelloWithFilter("Hana", filter)
}
```

```
Hello Alan
Hello Hana
```

## Contoh *code* 2

```go
package main

import (
	"fmt"
	"strings"
)

// function as parameter
func IsDirty(word string, filterWord func(string) bool) {
	if filterWord(word) {
		fmt.Println("That is dirty word")
	} else {
		fmt.Println("That is not dirty word")
	}
}
	
func main() {
	// anonymous function
	filterWord := func(word string) bool {
		if strings.Contains(word, "fuck") {
			return true
		} else {
			return false
		}
	}

	IsDirty("hi", filterWord)
	IsDirty("fuck", filterWord)
}

```

```
That is not dirty word
That is dirty word
```
