DEV Community

Cover image for Functions; what is it to python ?
EricKaranja17
EricKaranja17

Posted on

Functions; what is it to python ?

Python being one of the most popular programming language due to its relatively ease to use, while still extremely versatile and powerful. It is the go to object-oriented programing language for data gurus. If you are burning to learn Python, then understanding how to write functions is a good starting point.

In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name of the sequence of statements. Later, you can 'call' the function by name.

Function calls

marks= 10.2,100.10,16.17
type(marks)
Enter fullscreen mode Exit fullscreen mode

The name of the function is type. The expression in parentheses is called the argument of the function.
A function takes an argument and returns a return value/ result.

A Python functions consists of three components.

  • A def statement defining a function

def function_name(parameter1, parameter2):

  • Body: The block of code that executes when the function is called.
  • A return statement: used to send a value back from the function to the caller.

Below are various examples of the syntax above:

def add(a, b):
        result = a + b
        return result
Enter fullscreen mode Exit fullscreen mode
def my_function():
  print("Hello from a function")

my_function()
Enter fullscreen mode Exit fullscreen mode

Adding New Functions
A function definition specifies the name of a new function and the sequence of statements that run when the function is called:
Eg:

def lyrics():
        print("Back Bencher ndani ya S-Class.")
lyrics()
Enter fullscreen mode Exit fullscreen mode

def is a keyword that indicates that this is a function definition. The name of the function is lyrics. The rules of function names are the same as for variable names. Here is a synopsis of those rules: letters,number and underscores are legal, but the first character can't be a number. Keywords cannot be used as a name of a function. Avoid having a variable and a function with the same name.
The def statement should end with a colon and the body has to be indented. Indentation is always four spaces.

The syntax for calling the new function is the same as for built-in functions. Once you have defined a function, you can use it inside another function.
For example, to repeat the previous refrain, we can write a function called repeat_lyrics:

def lyrics():
    print("Back bencher ndani ya S-class")

def repeat_lyrics():
    lyrics()

repeat_lyrics()
Enter fullscreen mode Exit fullscreen mode

The program contains two functions definitions:lyrics and repeat_lyrics. Function definitions get executed just like other statements, but the effect is to create function objects.

Why functions?

Let me present to you a few reasons why functions are crucial in your program.

  • Creating a new function gives you an opportunity to name a group of statements,which makes your program easier to read and debug.
  • Functions can make a program smaller by eliminating repetitive code.Later, if you make a change, you only have to make it in one place.
  • Dividing a long program into function allows you to debug the parts one at a time and then assemble them into a working whole.
  • Well-designed functions are often useful for many programs. Once you write and debug one, you can reuse it.

Top comments (0)