How to define a function in python code? Let’s take a look :
Let’s examine the syntax. the def keyword which is used to create a function. After def keyword, the function name using parentheses and colon to start the function. Inside the parentheses, it is possible to define n number of arguments that can be used on function calls. Inside the function, there is the function code and possibly or not a return function at the end.
Example:
To call a function, you simply need to write the function name with the parentheses outside the defined function.
When we are defining a function, we can have different types of functions: built-in functions and user-defined functions. Built-in function is a function that is built into Python programming language and user-defined functions are functions that is created/defined by the users. For built-in function, as an example, we can have the str(), which converts any variable type into a string.
We can pass arguments to a function in order to get a specific result. In terms of function arguments, we can have 4 types of arguments: required arguments, keyword arguments, default arguments, and variable-length arguments.
• Required arguments: These types of arguments are passed during the time of function calling in order to run the function. These arguments are part of the function code, so, if the user doesn’t pass an argument to a function during the function calling, then an error will raise, more precisely a TypeError saying that an argument is missing.
• Keyword arguments: It is possible to call a function by passing keyword arguments. This allows us to call an argument in a random order (different from required arguments).
• Default arguments: On default arguments, we can initialize an argument when we are defining a function, it is not necessary to pass the argument because it has already a default value.
Output:
• Variable-length arguments: Sometimes you don’t know how many arguments you will need on your function. For that, you can create variable-length arguments on the function definition by defining (*arguments) inside the parentheses. Inside the function, the arguments are treated as tuples.
Output:
Anonymous functions are also known as lambda functions. An anonymous function is defined without a name and is defined using the lambda keyword. The syntax for anonymous functions is the following:
We can use any number of arguments in anonymous functions but in return, you can only use one expression. Let’s see an example of an anonymous function:
In terms of variable scope, we can have two types of scopes: global and local variables. A global variable is defined outside any function and a local variable is defined inside a function and a local variable is defined inside the function scope. Let’s take a look first to a global variable example:
Output:
In this example, we define the variable “number” outside the function, and we can call it inside the function and outside the function.
Now, as for the local variables, it is defined inside the function scope. If you try to call it outside the function, the python will raise a NameError exception where it says that the variable “x” is not defined.
Let’s take a look to how can we define a local variable:
In this example, the variable “number” is inside the function. If it is called outside the function it will raise an error as mentioned above.