Functions in Python
Explore how to define and use Python functions, including creating user-defined functions, recursive calls, and importing functions from other files. This lesson helps you understand the role of functions in building modular and manageable code essential for deep learning projects on Android.
What is a function?
A function is a piece of code that runs only when it’s called. We can input data as parameters to a function. We can also get data as output from a function. Our programs extensively use built-in Python functions. Some examples of built-in functions include range(), len(), round(), and print().
We can also define custom functions known as user-defined functions. In this lesson, we discuss how to create and use user-defined functions.
User-defined functions
The syntax for creating a user-defined Python function is given below.
def function_name(parameters):statement(s)return expression
Python uses the keyword def to define a function followed by the function name. The parameters are input to the function. When we pass real values to a function, they’re called function arguments. A function:
Might not have any parameter as an input.
Runs one or more
statement(s)and returns the output data as anexpression.Might not have a
returnstatement.Might return single or multiple outputs separated by commas. ...