How to create functions in python: Get square of a number.

Lets look how to create functions in python.

For example:- We will create two functions:
1. To say “Hello World” to the world.
2. To square a number and get its output.

Program

# Need to use def keyword
# then function name
# We need to make sure proper indentation is there

def hello(name):
    print("Hello "+ name)

# return statement when we want to return a value

def square(num):
    return num*num


hello("John")
hello("Kitty")
result=square(4)
print(result)

 

Output

/Users/saket/PycharmProjects/1helloworld/venv/bin/python /Users/saket/PycharmProjects/1helloworld/functions.py
Hello John
Hello Kitty
16

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments