Programming Essentials Python - User Defined Functions - Recap of User Defined Functions

User Defined Functions play a pivotal role in Python programming. In this article, we will delve into the various aspects of User Defined Functions in Python and how you can use them effectively.

Explanation for the video

Put a place holder for the video here with text so that I can replace as part of the automation

Key Concepts Explanation

Let’s explore some of the key concepts related to User Defined Functions in Python.

Defining the function with parameters

When defining a function in Python, you can specify parameters that the function will accept. Here’s an example:

def greet(name):
    print(f"Hello, {name}!")

Relevance of Doc Strings

Docstrings are used to provide documentation for functions and classes in Python. They are enclosed in triple quotes and can be accessed using __doc__.

def greet(name):
    This function greets the user by name.
    print(f"Hello, {name}!")

Returning one or more values

Functions in Python can return one or more values using the return statement.

def add(a, b):
    return a + b

Hands-On Tasks

Let’s put our knowledge of User Defined Functions to the test with the following tasks:

  1. Define a function add_employee that takes various parameters such as employee ID, name, salary, phone numbers, and degrees.
  2. Implement error handling to validate phone numbers, salary, and degrees.
  3. Display appropriate messages based on the validation results.


In this article, we explored the essential concepts of User Defined Functions in Python. By mastering these concepts, you can leverage the power of functions for building effective Python programs.

Recap of User Defined Functions

As we have gone through all the key concepts related to User Defined Functions, let us recap them.

  • Defining the function with parameters

  • Relevance of Doc Strings

  • Returning one or more values

  • Defining default values to the parameters

  • Passing argument by position

  • Keyword arguments or passing argument by name

  • Different types of special arguments and how they are passed.

    • Varying arguments are passed as tuple

    • Varying keyword arguments are passed as dict

Come back to this once you are done with collections where processing tuples, lists, dicts etc are extensively covered. When you are comfortable, create a function called as add_employee which will put all the concepts related to User Defined Functions in action.

  • Function should take employee_id, employee_name, salary, phone_numbers (variable number), degrees (variable keyword arguments) as parameters.

  • We should be able to pass multiple phone numbers as argument for phone_numbers

  • Degrees should be with specialization. There can be one or more degrees with specializations with keys bachelors, masters, executive, doctorate.

  • Make sure salary is defaulted to 3000. If salary is passed and if it is less than 3000 print a message Invalid salary: {salary}, salary should be at least 3000

  • To get invalid phone count, create a function get_invalid_phone_count which takes employee_id and varrying phone numbers as argument. The function should return employee_id and invalid_phone_count.

  • A phone number which have 10 digits or characters is valid otherwise it is invalid.

  • Call get_invalid_phone_count and check if invalid_phone_count is greater than 0. If invalid phone count is greater than 0, print a message {l_invalid_count} phone numbers out of {len(phone_numbers)} are not valid

  • Get count of degrees by processing variable keyword argument. If there are any invalid degrees print One or more degrees are not valid

  • If all the values passed as arguments are valid print Employee {employee_id} with {number} of degrees is successfully added and his salary is {}

Watch the video tutorial here