Python Functions

Python Functions

Functions plays an important role in simplifying the execution of a program. We can easily divide the work into different parts using functions. Main function will be in charge of calling each and every other function in the given order. Each function will perform a certain task and carry out the result of the function to the main, such that it can be given as input to the other calling function.

Rules for defining functions in python:

1. Every function should be defined with the def key word followed by function name and paranthesis() and ending with colon :.

2. Any arguments to the function should be declared in the paranthesis.

3. The comments for explaning working of the function is optional and if written should be in ” “.

4. The code should starts only after : and it should be intended.

5. The return statement is optional and return without parameters is same as return none.

def func_name( parameters) : //Declaring function and parameters
    code;
    code;
    return ; //Return statement if any

Sample function in python, addition of two numbers.

def add(num1, num2):
	num1 = int(num1)+int(num2);
	print("\n Addition of given two numbers is:" + str(num1));

num1 = input("\n Enter number 1 for addition:");
num2 = input("\n Enter number 2 for addition:");
add(num1,num2);

Output:
func
Download the Source code

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Python and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s