Function in Python

A function is a reusable code for specific action. Here we are defining two different function.

  1. First one is to say hello to the user after asking their name with their age.
  2. Second function is to calculate square of a number.
#defining a function
def say_hi(name, age):
    print("Hello" + name + ", you are " + age)

#asking user for their name and age: input
name = input("What is your name?")
age = input("What is your age?")

#calling the function
say_hi(name, age)

def sq(num):
    return num*num

#calling the function and printing the result

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s