Functions and Methods

In this post, we'll look at various aspects of Python's functions and methods.

Functions

Python lets us group a sequence of statements into a single entity, using functions. A function is a set of statements that take inputs, do some specific computation and produce output. In other words, a function is a block of code which only runs when it is called. They allow us to implement code reusability, which means you don’t have to write the code again and again each time you want to apply it. Python has two types of functions: built-in and user-defined functions.

User-defined Functions

Functions written by the programmer are known as user-defined functions. User-defined functions help to decompose a large program into small segments which makes program easy to understand, maintain and debug. Below is the syntax for writing user-defined functions in Python.

  • In Python, def keyword is used to declare user defined functions.
  • An indented block of statements follows the function name and arguments which contains the body of the function. The contents inside the body of the function must be equally indented.
Here's an example of how one might use a function:

Rules for naming a user-defined function

  1. The name can begin with any of the following: A-Z, a-z, and underscore(_).
  2. The rest of the name can contain any of the following: A-Z, a-z, digits(0-9), and underscore(_)
  3. You can't use a reserved keyword (i.e. print(), if(), etc)

Calling a Function

To call a Python function at a place in your code, you simply need to name it, and pass arguments, if any. See the example below:

Using Parameters in Functions

Sometimes, you may want a function to operate on some variables, and produce a result. Such a function may take any number of parameters. A function can take arguments (also called parameters) as input. These parameters go within the opening and closing parentheses, just after the function name followed by a colon. The syntax is as follows:

Function Return Statements

In Python, it is optional for a function to return a value. This value can be a result that is produced upon the method's execution, or it can be something you specify (i.e. an expression or a value). Here's an example: 
Note: As soon as a return statement is reached in a function, the function stops executing. Then, the next statement after the function call is executed. 

Deleting a Function

You can delete a function with the ‘del’ keyword, as seen in the example below:
When deleting a function, you don’t need to put parentheses after its name.

Built-in Functions

The set of functions that readily come with Python are called built-in functions. There are 60+ of them. This website briefly describes each built-in function, and you can click here if you'd like to find out more information on them.

Methods

A Python method is a label that you can call on an object; it is a piece of code to execute on that object. You create and work with methods in Python in the same way that you do functions, except that methods are always associated with a class. In other words, in Python, a method is like a function, except it is attached to an object. We call a method on an object, and it has the ability to makes changes to that object. You can create two kinds of methods: those associated with the class itself and those associated with an instance of a class.

Class Methods

A class method is one that you execute directly from the class without creating an instance of the class. Sometimes you need to create methods that execute from the class. Here is an example:
Note: A class method can work only with class data. It doesn’t know about any data associated with an instance of the class. You can pass it data as an argument, and the method can return information as needed, but it can’t access the instance data.

Instance Methods

An instance method is one that is part of the individual instances. You use instance methods to manipulate the data that the class manages. As a consequence, you can’t use instance methods until you instantiate an object from the class. Here's an example:
All instance methods accept a single argument as a minimum, self. The self argument points at the particular instance that the application is using to manipulate data. Without the self argument, the method wouldn’t know which instance data to use.

Difference Between Functions and Methods

As you've likely noticed, functions and methods look similar and perform in a similar way. However, they do have distinct characteristics that makes them different. Functions can be called only by their name, as they are defined independently. But methods cannot be called by their name only, as you need to invoke the class by a reference of the class in which the method itself is defined.

Your turn!


Sources: 

https://data-flair.training/blogs/python-method-and-function/
https://data-flair.training/blogs/python-function/
https://www.geeksforgeeks.org/python-user-defined-functions/
https://www.dummies.com/programming/python/working-with-methods-in-python/
https://www.w3schools.com/python/python_functions.asp

Comments

  1. I noticed the delete function, do you have any knowledge on what the reasoning for implementing that is? I'm just curious as to why someone would use that instead of simply cutting the code or commenting it out of the program.

    ReplyDelete

Post a Comment

Popular Posts