Python Programming Function Tutorial with Stone River eLearning

It is quite understandable that beginners always shy away from using functions in their codes and often prefer writing large number of lines in a program to grouping the functionalities into different functions. Functions is no doubt a great utility in computer programming and its advantages are much more than reducing the line of code and making the program more presentable and structured. Before, we discuss why we should use functions and how to use functions in Python, let us talk about the points that make functions such an important and integral part of any program.

  1. Disintegrates huge problems into smaller pieces from where the platform of the whole program or application can be laid.
  2. Enables reuse of the code in case of programs where a same set of operation is required to be performed on different inputs
  3. Building on the point above, functions ensure that there is no duplicity in the code and unnecessary lines are avoided with its help.
  4. From a security point of view, grouping the code into different functions ensures that the important information or code structure is not exposed and also the chances of a fault in the functioning of the program is less.

For any programming language, a function specifies that block of code which can be used anywhere in the program given it has been declared in the same program.  From a more practical perspective, imagine you have been given the task of writing a Python program that calculates the number of alphabets in a string that is entered by the user, but ignore any integer values that come in between. No doubt we can achieve such a program without using functions as well, but imagine how many lines of code it would take and the number of flow control statements. It is get quite messy and debugging such a program would be a painful task. However; if we are using functions, we could easily write different code for a string with only alphabets and the one with integers in it as well and put them in different functions. Now finding the set of code which is not working and correcting the error in it is easy and faster.


def example():




if x<y:

print(x,’is less than’,y)

def main():


Points to Ponder:

  1. There are a few important points to note about using function in Python. First of all, declaring a Python function is a little different from how functions are declared in other programming languages. To declare a function, we must use the keyword “def” before the name of the function in the declaration part to tell Python that you intend to start a function declaration.
  2. Since we have mentioned this numerous times in the previous blog posts, it goes without saying that using the colon (:) symbol is necessary and critical to the correct execution of the Python code.
  3. Once you have declared the function and its operation, the next thing is making the function call. A function call is typically giving a green signal to the Python that now is the time to execute the operation inside the function.
  4. To make a function call, we just have to type the name of the function followed by the open and closed parenthesis. We can even call a function inside any other function like it is done in the example above or we can call it anyplace else on the same program.

Online Technology Courses Stone River eLearning