Monday, March 2, 2026

Python Tutorial for Beginners: Functions

In the previous blogs/chapter, we learned that small code increases a program clarity to read and modify. As we continue learning Python, we will see large codes. Now assume, we may need to write the same code again and again, it becomes very tedious task for programmer/coder.

To handle this issue, we use function which makes program clearer, well-structured and more understandable. Also, it is used anytime and anywhere within the program.

What is Function in Python programming?

Function is a block of code that we can use it in our existed code when needed.

Syntax:

def function_name():

    code

Here: def: Use to define a function, function_name: name of the function, (): parentheses (can hold parameters), :  start of function block

Example:  

def welcom_msg():
    print("Hello Guys, Welcome to Quillix Blogspot")
welcom_msg()

function

Perameter and Arguments

In a function definition, parameters are variables inside parentheses and arguments are values ​​that are passed when the function is called.

Example:

def pmter_argu(Name):                 # Name - is Parameter
    print("Hello," Name)
pmter_argu("Somit")                   # Somit - is argument

argu

Return Statement

It executes the function and send the value back to it whenever the function is called.
Example:

def sum_two(a, b):
    return (a+b)
x = sum_two(10, 20)
print(x)                        #Output: 30

return

Default Parameter

You can also give a default parameter value that executes when user does not provide any value.

Example:

def df_ptm_val(name = "Guest"):        #default value
    print ("Hello", name)
df_ptm_val()                #Output: Hello, Guest

default parameter

Multiple Value Return

In python, you can also return multiple value at once.
Example:
def multi_val(a, b):        
    return a+b , a-b  #multiple value return
x = multi_val(10,5)
    print(x)                 #Output: 15, 5
multi value return

Local and Global Variable

Local variables are used within a function and can only be accessed inside the function. While Global variables used for whole programm and can be used anywhere when needed.
Example:
x = 10             #Global Variable
def num():
    y = 5			#Local variable
    print(y)
    print(x)
num()                    #Output: 5 10
local and global

Create Simple Calculator

print("Simple Calculator")
def calculator():
    a = int(input("Number 1: "))
    b = int(input("Number 2: "))
    operator = input("Operator(+ or -): ")
    if operator == "+":
        print (a+b)
    elif operator == "-":
        print (a-b)
    else:
        print ("Only Addtion and Substraction")
calculator()
calculator project
Task 1: Create function to Multiply Two numbers.
Task 2: Create function to return Multiple values.
Task 3: Add multiplication and divide option in Calculator.

Final Thought

Python functions are the most important chapter; without functions, we can't even imagine using the same program over and over again. So, learn them carefully and practice as much as possible.


No comments:

Post a Comment

Featured Post

Python OOPs Advanced: Class Variables and Instance Variables

We have learned how to create classes and objects . Now, we will explore class variables and instance variables in Python, including how the...