Python Function.. Parameter, Arguments,Factorial of number.

Function:

It is difficult to maintain large scale program and the identification of the programming gets harder. The best way to create programming application is to divide a big program in small module and repeatedly call this module within program with the help of function and entire program can be devide into small independent modules.

A function is the self contained block of one or more statement that perform a special task when called. This improve the code readability as well as flow of execution and small modules can be managed easily.

Syntax:

def function_name (list_parameter): statement(s)

the syntax for the python contains header and the body. The function header begins with the header 'def' followed by the function name and arguments are optional. the body contents the statement to be executed and function will be executed only when it is being called.

Example:

Write a python program to display a simple message

Source code:

def showFunction (): print("This Python function") showFunction()

Output:

This Python function

Function use:

The functions are used for code reusability in program. Assume that a program is written for printing the number starting from 1-5,20-25 and 90-95

def print_numbers(start_number,end_number): for num in range(start_number,end_number+1): print(num) print("The sequence of numbers:") print_numbers(1,5) print_numbers(20,25) print_numbers(90,95)

Output:

The sequence of numbers: 1 2 3 4 5 20 21 22 23 24 25 90 91 92 93 94 95

Parameter and Argument in Function:

Parameters are used to give input to the function in programs. They are specified with the pair of parenthesis in function ddefinition. When program calls a function, the values are also passed with the function.

While parameter are defined by name, arguments are value actually passed to a function when calling it. Thus parameter define what type of arguments a function can except.

Example:

Write a python program to find the factorial of a number.

def factorial(num): fact=1 for i in range(1,num+1): fact=fact*i print("factorial of number:",fact) numb=int(input("Enter a number to find factorial:")) factorial(numb)

Output:

Enter a number to find factorial:5 factorial of number: 120

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.