In Python, a function is a block of organized and reusable program code that performs a single, specific, and well-defined task.
Every function encapsulates a set of operations, and when called, it returns the information to the calling program.
Python Function Definition
Any function can be compared to a black box (that is used for an entity having unknown implementation) that takes in input, processes it, and then spits out the result. However, we may also have a function that does not take any inputs at all or that does not return anything at all.
A function definition consists of a function header that identifies the function, followed by the body of the function containing the executable code for that function.
To define a function in Python, we must follow the rules:
- Function blocks start with the keyword def.
- The keyword is followed by the function name and parentheses (()). The function name is used to identify the function uniquely.
- After the parentheses, a (:) is placed.
- Parameters or arguments that the function accepts are placed within parentheses. Through these parameters, values are passed to the function. They are optional. In case no values are to be passed, nothing is placed within the parentheses.
- The code block within the function is properly indented to form the block code.
- A function may have a return statement. That is, the return statement is optional. If it exists, it passes back an expression to the caller. A return statement with no arguments is the same as a return none.
- We can assign the function name to a variable. This helps us to call the same function using the name of that variable.
Syntax of a function definition is given below:
def function_name(variable1, variable2,..)
documentation string
Statement block
Return [expression]
Example 1:
def add(x,y): #function to add two numbers return(x-y) x=90 y=80 addition=add #function name is assigned to a variable print(addition(x,y)) #function calling using variable name
Output:
10
Example 2:
def fun(): for i in range(5): print("Python is a Language") fun() #function call
Output:
Python is a Language Python is a Language Python is a Language Python is a Language Python is a Language
Python Function Call
Defining a function means specifying its name, parameters that are expected, and the set of instructions. Once the basic structure of a function is finalized, it can be executed by calling it.
The function call statement invokes the function. When a function is invoked, the program control jumps to the called function to execute the statements that are a part of that function. Once the called function is executed, the program control passes back to the calling function.
The syntax of calling a function that does not accept parameters is given below:
function_name()
The syntax of calling a function that accepts parameters is given below:
function_name(variable1, variable2,..)
When the function is called, the interpreter checks that the correct number and type of arguments are used in the function call. It also checks the type of the returned value.
Python Function Parameters
A function can take parameters that are nothing but some values that are passed to it so that the function can manipulate them to produce the desired result. Actually, these parameters are normal with a small difference that the values of these variables are defined when we call the function and are then passed to the function.
The parameter list in Python must be separated by commas.
Example 3:
def f1(a,b): print(a,b) f1(5)
Output:
Error
Example 4:
def f1(a): print("Hello World",a) f1(5)
Output:
Hello World 5
Leave a comment