Skip to main content

Functions


FUNCTIONS
Functions are the already defined part of the program which can be recalled from other parts of the program as many times as needed. Functions plays an important role in helping to reduce the program code.

Function has basic three parts:
1. Function Definition
2. Function Call
3. THE FUNCTION DEFINITION

The FUNCTION DEFINATION:
The function definition deals with purpose/task of the function. It tells the python what the function does.
Syntax:
Def function_name(arguments):
Function body.
Example:
Def Add(a,b):
Print(a+b)
Arguments are the variables used in function. You cannot skip variable name in function definition.

THE FUNCTION CALL
Function call passes the variable values to function definition, the number of values must match the number of parameters.
Syntax:
Function_name(values)
Example:
Add (5,6)
Gives output
11

RETURN STATEMENT:

Return statement can be used to return function result to function call:
Def add(a,b):
Return (a+b)
Now return sends the value of sum back to function call this value may be
further used in program.
For example:
functionanswer = Add(6,11)
is equal to int functionanswer = 6+11

Comments

Popular posts from this blog

Features of python

  Python   is a dynamic, high-level, free open source, and interpreted programming language. It supports object-oriented programming as well as procedural-oriented programming. In Python, we don’t need to declare the type of variable because it is a dynamically typed language. For example, x = 10 Here, x can be anything such as String, int, etc. Features in Python There are many features in Python, some of which are discussed below – 1. Easy to code: Python is a high-level programming language. Python is very easy to learn language as compared to other languages like C, C#, Javascript, Java, etc. It is very easy to code in the python language and anybody can learn python basics in a few hours or days. It is also a developer-friendly language. 2. Free and Open Source: Python language is freely available on the official website and you can download it from the given download link below click on the  Download Python  keyword. Download Python Since it is open-source, this means that source

Conditional Statements

Conditional STATEMENTS Normally, the statements are executed sequentially as written in the program (i.e. Top to bottom). This is known as the normal flow of control. But sometimes the operational flow of control has to be altered depending upon a condition. The statements that allow to do so, are known as selection statements. Most important selection statement that python offers is the If statement. Selection statements are also known as conditional statements or decision statements. THE IF STATEMENT An if statement tests a particular condition; if the condition evaluates to true, a course of action is followed i.e. a statement of a set of statements is executed. If the condition is false the course of action is ignored. The syntax of if statement is as shown below: if (expression) : Statement THE ELIF STATEMENT In some cases the program requires you to write multiple if statements. This results in many condition evaluations thus slowing up the program. So to avoid th

Loop Statements

Loop STATEMENTS The Loop statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. THE FOR LOOP The for loop is the easiest to understand of the python loops. Syntax: For I in range(lowerlimit,upperlimit): Loop Body The program will run until I value becomes equal to upperlimit – 1. Example: For I in range(1,5): Print(“Hello”) This program will print Hello 4 times, ie from 1 to 4 (upper limit-1)  I value will start as 1 then loop body will get executed and I value will be incremented. This keeps on taking place until I ==upper limit-1 . After that the program comes out of loop body. THE WHILE LOOP The second loop available in python is the while loop. The while loop is an entry controlled loop. The syntax of a while loop is while (expression): Loop-body Where loop body keep on getting executed until the expression becomes false. When the expression becomes false, the pr