Functions in Python
Functions in Python
Function with No Parameters in Python A function with no parameters has an empty pair of parentheses after its name in the function definition. For example:
This is the output when we call the function:
Function with One Parameter in Python A function with one or more parameters has a list of parameters surrounded by parentheses after its name in the function definition:
When we call the function, we just need to pass one value as argument and that value will be replaced where we use the parameter in the function definition:
Here we have another example – a function that prints a pattern made with asterisks. You have to specify how many rows you want to print:
You can see the different outputs for different values of
:
Functions with Two or More Parameters in Python To define two or more parameters, we just separate them with a comma:
Now when we call the function, we must pass two arguments:
We can adapt the function that we just saw with one parameter to work with two parameters and print a pattern with a customized character:
You can see the output with the customized character is that we call the function passing the two arguments:
How to Return a Value in Python Awesome. Now you know how to define a function, so let’s see how you can work with return statements. We will often need to return a value from a function. We can do this with the
statement in Python. We just need to write this in the function definition:
Here we have an example:
Now we can call the function and assign the result to a variable because the result is returned by the function:
We can also use
with a conditional to return a value based on whether a condition is
or
. In this example, the function returns the first even element found in the sequence:
If we call the function, we can see the expected results:
The Style Guide for Python Code recommends using return statements consistently. It mentions that we should: Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable) Default Arguments in Python We can assign default arguments for the parameters of our function. To do this, we just need to write
in the list of parameters. In this example, we assign the default value 5 to the parameter
. If we omit this value when we call the function, the default value will be used.
If we call the function without this argument, you can see the output:
We confirm that the default argument 5 was used in the operation. But we can also assign a custom value for
by passing a second argument:
Here we have another example with the function that we wrote to print a pattern. We assign the default value
to the
parameter.
Now we have the option to use the default value or customize it:
S.Schins BASICS
Python Basics Examples