Functions in Python

 Functions in Python:

A function is a block of code which performs a specific task. It can be reused by calling it again whenever it’s necessary. It avoids the repetition of the code. 

Syntax :

def function_name(parameters):

# statement

return expression

‘def’ is a keyword, function_name is the unique name given to the function to identify the function and parameters or arguments are passed inside the brackets and ends with colon. Next line starts with indentation and the statement is given. At the end it returns the value.

Creating and calling a simple function in python:

The function is created by starting with def followed by the function name and then the parentheses inside which the parameters or the arguments are passed. 

Function can be created without passing any arguments or by passing arguments. If you don’t pass any arguments, it doesn’t return any value and if you pass the argument then function will return the value. 

Without arguments:


Creating Python function without arguments

With Arguments:


Creating python functions with arguments

Examples of Python functions:

Example 1: To find whether the given number is even or odd:


Python function to find the number is odd or even

Example 2: To find the greater number:

Python function to find the greater Number

Example 3: To find square of numbers from 1 to 10

Python function to find the square of numbers from 1 to 10


Post a Comment