A function is a block of code that performs an operation, usually a specific task.
Functions help break our program into smaller and modular chunks, making them reusable and readable. They prevent the repetition of code.
In programming, we use functions to divide code into modules. Modules :
- keeps code organized
- intended to be reusable
- used to break up code.
You can pass data known as parameters, into a function and a function can also return data as a result.
Writing a function in Python.
A function definition consists of the function’s name, parameters, and body.
function characteristics. has:
2. parameter (0 or more)
3. docstrings (optional but highly recommended)
4. a body
5. returns something - you are allowed to return only one object
Syntax of a python function.
def function_name(parameters): # function body
def begins a function definition. It should be followed by the function name and a parenthesized list of formal parameters.
The statements that form the body of the function start at the next line and must be indented.
Basic Example of a function..
def greet(name): """ This function takes a person's name as an argument and prints a greeting with the name """ print("Hello " + name)
The function is called
greet with one parameter,
The first string after the function header is called the docstring and is short for documentation string. It is briefly used to explain what a function does. It’s good practice to include docstrings in code that you write, so make a habit of it.
The last line of the function body is printing Hello with the name passed into the function when it is being called.
Functions are not run in a program until they are called or invoked.
Once we have defined a function, we can call or invoke it from anywhere within our program; but the function must be defined before it is called.
>>> greet("John") Hello John
Another Example of a Function.
Suppose we want to write a function that takes two numbers and finds the minimum of them.
The name of our function will be min.
def min(num1, num2): if num1 < num2: result = num1 else: result = num2 return result # returns the minimum number
Invoking a Function.
To use a function, you have to call or invoke it. There are two ways to call a function, depending on whether it returns a value or not.
If the function returns a value, a call to that function is usually treated as a value. For example our
min() function returns a value, therefore a call to the
min() function is treated as a value.
Calling the min function:
small = min(3, 9) # the min function called will return a value which will be stored a variable, small.
You can then verify this by :
print(small) # 3
Alternatively, you can straightly print out the value returned from the function without using a variable.
print(min(3, 9)) # 3
Note this approach of calling a function is for only functions that have a return statement.
Functions like the greet() function does not have a return statement but has a print statement. A call to this type of function without a return statement is treated as a statement, not a value.
For example, calling or invoking the greet function:
greet("doe") # Hello doe
Note the greet function does not return any value, so it cannot be stored in a variable like the min() function. It provides the same output when you print it:
print(greet("doe")) # Hello doe
If you try to treat the greet function as a value, it returns none.
Parameters or Arguments.
The terms parameter and argument can be used for the same thing: information that is passed into a function.
More Specifically :
A parameter is the variable listed inside the parentheses in the function definition.
An argument is a value that is passed to the function when it is invoked or called.
A function with both print and return statement.
Though most functions are designed to return statements, sometimes you might need some print statements in them for debugging purposes or to evaluate functions.
For example, a function that checks whether a number is even or not.
def IsEven(x): """ this function takes an integer as an argument, checks if it is an even number and then returns a boolean value """ print("hey i am inside isEven function") # print statement if x % 2 == 0: even = True else: even = False return even # return statement # Invoking the function IsEven(3) # prints only the print statement print() # just generates space in output print(IsEven(4)) # prints both the print and the return statement
Try writing and running more program. It is the best way to understand programming.