MOCKSTACKS
EN

# Raspberry Define Function

In mathematics, “function" refers to a corresponding relationship and change process between values. In the field of programming, a function is a program with specific functions and can complete specific tasks. When we complete this program, we can call it repeatedly in later programs to deal with the same task without writing more code!In the previous lessons, you have used many Python built-in functions, such as print(), pin(), etc. Similarly, we can also use our own custom functions.

In Python, we use “def" to define functions. The syntax of defining functions is as follows:

``````def function name (parameter list):
function body``````

When defining a function, the function code block starts with “def", followed by the function name and the parenthesis “()". The colon “:" after the parenthesis indicates the beginning of the function body. The function body contains all the code that define a function. When you complete the function definition, the system can execute the code in the function body every time you call the function.

Let's look at the example program:

``````def HelloWorld():
print("Hello World")

HelloWorld()``````

The execution result of the program is as follows:

``Hello World``

In this program, we define a function named “HelloWorld". Every time we call the function with “HelloWorld()", we execute the “print(“Hello World")" statement in the function body.

In the brackets after the function name, we can also write the parameters that may need to be used by the function, for example:

``````a = 4
b = 5
def max(a, b):
if a > b:
return a else:
return b
print(max(a, b))``````

In this program, we create a function named “max()", which introduces two parameters into the parameter list. In the function body, the program compares the two numbers and returns the larger number.The “return [expression]" represents the end of the function, and it returns a value to the function calling it. For example, in this program, we judge the larger number between a and b through the “if" condition and return the chosen value. Therefore, the execution result of the program is 5, that is, the value of b which is greater.

``5``

And that's how functions are defined.

## Conclusion

In this page (written and validated by ) you learned about Raspberry Define Function . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Playing Basic Notes.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.