Python Functions (A Beginner’s Guide)
Python Functions are a block of code that is designed to repeatedly perform a specific task. We can write a group of statements inside a function which only execute when we call. We can call a function as many times we want, this makes our code shorter and easy to understand.
Functions help in organizing code, making it more modular, readable and maintainable. Functions also follows the DRY (Don’t Repeat Yourself) rule of programming, by allowing programmers to reuse code instead of duplicating it.
In Python, a function is created using the
def keyword. Here is a Syntax of a python function:
def function_Name(): #some code...
In an example below let’s write a code, and create a function namely hello, clear your doubts about Python Functions.
def hello(): print("Hello friend, soon you will become expert in python") #calling a function hello()
In an above example, we first declared a hello function, with a
def keyword. Then we write some code, and at the end we call the function by it’s name, and it executes the print statement written inside the function. You can call this function as many times you want and anywhere in the code, it will execute the same code all the time.
Python Functions with Arguments (Parameters)
Arguments in Python Function are defined inside the parentheses, after the function name. We can add as many arguments we want to add in a function. These arguments allows us to pass data into the function when we call it. Below in an example let’s see how it works:
def student(age): print("Your age is", age) student(12) student(17) student(10)
Your age is 12 Your age is 17 Your age is 10
In an above example we created a function namely
student, which takes one argument (age). Then inside the student function, the print statement prints the string “Your age is” followed by the value of the
age argument. At last we call the
student function three times with different age values. Each time the function is called, it prints a message indicating the age provided as an argument.
Python Functions with Multiple Arguments
As mentioned earlier, we can add as many arguments we want to add in a function. So now let’s add more then one argument in a function and see how it works.
def student(name, age): print("Students name is", name, "and his age is", age) student("Abdul", 12) student("Ram", 17) student("John", 10)
Students name is Abdul and his age is 12 Students name is Ram and his age is 17 Students name is John and his age is 10
In an above example, the
student function takes two arguments (name & age). Then inside the student function, the print statement prints the string “Students name is” followed by the value of the
name argument and the string “and his age is” followed by the value of the
age argument. At last we call the
student function three times with three different name and age values. Each time the function is called, it prints a message indicating the name and age provided as an argument.
return Statement in Python Functions
In Python Functions, the
return statement is used to send a value back from the function to the caller. Here is an example
def multi(a, b): return a *b print (multi(3, 4)) print (multi(2, 6)) print (multi(5, 5))
In an above example, the function named
multi takes two arguments,
b, and returns their product (
a * b). Then we call the function three times with three different pairs of numbers and prints the results.
The Output of this code is:
12 12 25
pass Statement in Python Function
In Python functions, the
pass statement is used when we want to define a function without providing any code implementation yet. It’s a way to create a function skeleton that can be filled in later. Here’s how the
pass statement is used within a function:
def function_name(); pass
Share This Post!