给定一个英文句子，编写一个函数，该函数将返回该句子中最长的单词。如果有多个单词具有相同的长度，则返回第一个遇到的单词。输出函数代码。


句子：This is a sample sentence to test the function.
```python
def longest_word(sentence):
    words = sentence.split()  # split sentence into words
    longest = max(words, key=len)  # find the longest word using the max function with key=len
    return longest  # return the longest word
```

使用方式：

```
sentence = "This is a sample sentence to test the function."
longest = longest_word(sentence)
print(longest) # Output: "sentence"
```