DEV Community

Cover image for Introduction to Python functions.
Iankips
Iankips

Posted on

Introduction to Python functions.

Functions.

These are reusable piece of code that performs a certain task when called up. They aid in breaking down of code into smaller and modular chunks. They furthermore make the code more manageable and organized as the program grows. For the major part it is used to avoid repetition.
In python, they are of two types;
In-built functions.
User defined functions.

In-built Functions.

These are defined as functions whose functionality is predefined in python. The python interpreter has several functions that are always readily available for use whenever the need arises. Some examples of these built-n functions are;

Python abs() function.

This function is used to return the absolute value of a number. It only takes one argument and that is the number whose absolute value is to be returned. The argument can either be an integer or a floating point number. If the argument happens to be a complex number, then the abs() function returns its magnitude.
The abs() function, furthermore, ignores a negative sign that precedes a number. This is to mean that if the number is for example -4 abs() returns 4. Returns positive numbers.

# Integer.
a = -34
print(abs(a))
# prints 34.
# floating point.
 b = -45.67
print(abs(b))
#prints 45.67
Enter fullscreen mode Exit fullscreen mode

Python float() function.

This is a built in python function that is used to convert a string or integer to a float point value. The float() function takes one parameter, that is the value you want to convert to a float. Passing on the parameter in this case is optional. The default value is 0.0 .
Converting an integer to a float in python is achieved as in the following example.

a = 45
print(float(a))
# prints 45.0
Enter fullscreen mode Exit fullscreen mode

And converting a string, which is nothing but just a sequence of characters, is don as in the example below.

b = '32'
print(float(b))
# prints 32.0
Enter fullscreen mode Exit fullscreen mode

Python sum() function.

This is a python function that sums up all the numerical values in an iterable such as a list and returns the total of such values. The sum() function calculates the total of both floating point numbers and integers.
The sum() function can take two parameters;
a. The iterable object that you would like to calculate the total of, this parameter is required.
b. An extra number you want to add to the value you are calculating, this is optional.

a = [1,2,3,4]
total = sum(a)
print(total)
# prints 10
# two parameters.
a = [1,2,3,4]
total = sum(a, 1)
print(total)
# prints 11.
Enter fullscreen mode Exit fullscreen mode

User Defined Functions.

As the name suggests, these are created by the user. These functions are created in a certain way unlike the in-built functions. User defined functions involves the use of keyword. The def keyword is used to create these functions. It is followed by a function name declared by the user. The function name is subject to the rules governing the naming of variables.
The following is a simple syntax to define function;

def hello():
    print("Hello world!")
# prints hello world
Enter fullscreen mode Exit fullscreen mode

The above code is a simple function with no parameters and arguments passed.

Parameters.

These are variables listed inside the parenthesis in function definition.

Arguments.

These are actual values that are passed on to the parameters.

Function with parameters and arguments.

def name(name):
    print(name + ,'Doe')
hello('John')
Enter fullscreen mode Exit fullscreen mode

In the above code, name is the parameter while John is the argument passed.

A function can have more than one parameter.

def country(name1,name2):
    t = name1 + ' ' + name2
    print(t)
country('Kenya', 'Uganda')
Enter fullscreen mode Exit fullscreen mode

Return statement is used to end the execution of a function and return a value.

def add(a, b):
    x = a + b
    return x
add(4,5)
print(x)
Enter fullscreen mode Exit fullscreen mode

Arbitrary arguments.

In the case you do not know how many arguments will be passed to the parameter, add * before parameter name on function definition.

def family(*kids):
    print('Eldest child is, ' + kids[0])
family('John','Gillian')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)