Functions in Python.

Python Functions:

In python, function is a group of related statements that perform a specific task. A function is a block of organized, reusable cod that is used to perform a single, related action.

Functions provide better modularity for your application and a high degree of code reusing.

Types of Functions:

i)Built in Functions

ii) User defined Functions

Built in Functions:

You already know, Python gives you many built in functions like print().

User defined Function

You can also create your own functions. These functions are called user defined function.

Example:

def swap(x,y): '''creating user temp=x define swap x=y function''' y=temp return x,y x=2 y=3 print(swap(x,y)) #calling function

Outupt:

(3,2)

Defining a Function:

Function Definition provides the information about function name, parameters and the definition what operation is to be performed. 'def' keyword is used to start and declare a function, it specify the starting of function block.

'def' is followed by function-name followed by parenthesis. Parameters are passed inside the parenthesis. At the end a colon is marked.

Syntax

def user_function_name(parameters): Block(s)

Example:

def sum(a,b): C=a+b

Calling a Function:

A function is needs to be called. This is called function calling. In order to execute the function definition definition,we need to call the function.

Syntax

user_function_name(parameters)

Example:

print(sum(4,5))

Python Function return statements:

return[expression] is used to return response to the caller function. We can use expression with the return keyword. It Send back to the control to the caller with the expresion. In case no expression is given after return it will return None. In other words statement is used to exit the function definition.

Example:

def sum(a,b): return a+b print(sum(9,4))

Output:

13

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.