Python – Functions

Function is a block of code that works on variable input and produce output. Functions provide better modularity for your application and a high degree of code reusing.

Features of Functions

  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • A function can return data as a result.

Creating a Function

In python def keyword is used to define a function

def myFunction():
	print("Hello from function")

Calling a Function

To call the particular defined function use the name of the function followed by ().

# Calling a Function
myFunction()

Passing Parameter to Functions

You can pass data, known as parameters, into a function.

# Passing Parameters
def myFunction1(fname="India",message="How are you?"):
	print("Hello",fname,message)
myFunction1("Huzaif","How are you?")
myFunction1()

Return values from Function

A function can return data as a result.

# Return Value
def myFunction2(num):
	return 4 * num

x = myFunction2(5) # returned value store in x
print(x)
Filed under: Python

No comment yet, add your voice below!


Leave a Reply