Python
programming
Defining Your Own Functions
We can define our own functions in Python and
reuse them throughout
the program. The syntax for defining a function is as
follows:
def functionName(parameters): code detailing what
the
function should do return [expression]
There are two keywords here, def and return.
def tells the program that the indented code from the
next line onwards is part of the function. return is the keyword that we
use to return an
answer from the function. There can be more than
one return
statements in a function. However, once the function
executes a return
statement, the function will exit. If your function
does not need to return
any value, you can omit the return statement.
Alternatively, you can
write return or return None.
Let us now define our first function. Suppose we
want to determine if a
given number is a prime number. Here’s how we can
define the function
using the modulus (%) operator we learned in
Chapter 3 and the for
loop and if statement we learned in Chapter 6.
def checkIfPrime (numberToCheck): for x in
range(2, numberToCheck): if (numberToCheck%x == 0):
return False return True
In the function above, lines 2 and 3 uses a for loop to
divide the given
parameter numberToCheck by all numbers from 2 to
numberToCheck
- 1 to determine if the remainder is zero. If the
remainder is zero,
numberToCheck is not a prime number. Line 4 will
return False and the
function will exit.
If by last iteration of the for loop, none of the
division gives a remainder
of zero, the function will reach Line 5, and return
True. The function will then exit.
To use this function, we type checkIfPrime(13) and
assign it to a
variable like this
answer = checkIfPrime(13) Here we are passing in 13 as the parameter. We can
then print the
answer by typing print(answer). We’ll get the
output: True.
Python Programming (Defining Your Own Functions)
of 4
Report
Tell us what’s wrong with it:
Thanks, got it!
We will moderate it soon!
Free up your schedule!
Our EduBirdie Experts Are Here for You 24/7! Just fill out a form and let us know how we can assist you.
Take 5 seconds to unlock
Enter your email below and get instant access to your document