# Passing Function to Spark

Spark's API relies heavily on passing functions in the driver program to run on the cluster. There are two recommended ways to do this:

Anonymous function syntax, which can be used for short pieces of code (In Python, it is called lambda function)

```
val rdd=sc.parallelize(Seq(1,2,3,4,5))
rdd.map(x=>x+1)
```

Static methods. For example, you can define function called myFunc, as follows:

```
def myFunc(x: Int): Int ={
    x+1
}
val rdd=sc.parallelize(Seq(1,2,3,4,5))
rdd.map(myFunc)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://george-jen.gitbook.io/data-science-and-apache-spark/passing-function-to-spark.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
