In this article, I’ll be talking about functions in python. You'll learn about functions, what a function is, the syntax, components, and types of functions.
Like most high level programming languages, python has a rich function, but what are functions?
Functions are a set of actions that we group together, and give a name to. In other words, a function is a block of code that is invoked when using a calling program.
The following characteristics exist for a function;
A function is named
A function is independent
A function performs a particular task
Types of Functions
Basically, we can divide functions into the following two types:
- Built-in functions - Functions that are built into Python.
- User-defined functions - Functions defined by the users themselves.
function can return a value to the calling program
Now let’s consider the very first program most of us wrote at the beginning of our bootcamp;
This program uses the print() function for displaying text on the screen, we call it by typing print(“Hello World”) where print is the name of the function and “Hello World” is the parameter.
We can also use functions to replace strings, for instance if we want to use the replace() function for manipulating text strings, we have to type.
“Hello World”.replace(“World” , “Universe”).
where replace is the name of the function and “World” and “Universe” are the parameters. The string before the dot (i.e. “Hello World”) is the string that will be affected. Hence, “Hello World” will be changed to “Hello Universe”. These are some of the ways we can use functions in python.
We can define our own functions, which allow us to "teach" Python new behavior.
The syntax for defining a function is as follows:
def functionName(parameters): code detailing what the function should do return [expression]
- Keyword def that marks the start of the function header.
- A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python.
- Parameters (arguments) through which we pass values to a function. They are optional.
- A colon (:) to mark the end of the function header.
- One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces).
- An optional return statement to return a value from the function.
Here is an example of how we can define a function reality
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, for loop and if statement we learned in first and second week of the bootcamp.
def checkIfPrime (numberToCheck):
for x in range(2, numberToCheck):
if (numberToCheck%x == 0):
return False return True.
In the function above, line 2 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.
I believe that with what I’ve shared today you are ready to venture more in using functions in python.
SAMUEL PETER OKOCI