Any large program is usually broken down into smaller parts to ease the dry running of that program. A large program is broken down into smaller units called functions. A function is a subprogram that acts on data often returning a value in the process. The usage of functions in a program is just to make it simpler to read and debug the program. In this way the length of the program also reduces.

Function In Mathematics:-

In mathematics, we have a concept of polynomials. Supposing we take a function of x such that at every interval it is equal to 3x^3. Implies that if we take x as 1 we get 3, if we take x as 2 we get 24, and so on. The same concept can be used in a python program. Whose syntax is as follows:-
Here def is used to define the function and the identifier following the def function is the name of the function. The variable inside the round brackets(also called parenthesis) is called the argument or parameters of the function. The statements written under the function are called the function body. and the return statement just like the print statement prints the compounded result of the argument.

Invoking/Calling of a Function:-

Taking a maths example if f(x)=2x^2 then f(1)=2. The same thing can be done in python by invoking a function. The syntax is:-
<FunctionName>(<Value To BE Given>)
For example, if the name of the function is square and if the value is 5 then it is written as- square(5).
Not let’s see different ways of Calling a Function:-
Take this snippet into consideration:

def square(x):
       r=x**2
        return r
def square(x):
    y=x**2
square(3)
n=5
square(n)
n1=int(input("ENTER A NUMBER"))
square(n1)
print(square(4))
d=5*square(6)

Now there are four ways of doing this:
(1) square(3)#directly substituting it as an argument
(2)square(n)#using a variable as an argument
(3)n1=int(input(“Enter a number:”))
square(n1)#accepting from the user and then calculating it
(4)print(square(3))#using invoke statement inside a print statement
(5)x=5*square(3)#assignment of the value of a function to a variable

Defining Functions:-

A function once defined can be called as many times as needed by using its name without rewriting the code again.
In a syntax language:
Anything inside angle brackets<> has to be provided by the programmer.
Anything inside square brackets[ ] is optional
Anything outside <> and [ ] have to be written as specified.
Function Header in the above image is the first line of the function definition
Parameters are the variable/constants within the parenthesis/ round brackets of the function header
Function Body is the block of statements under the Function Header
Indentation is left at the beginning in each statement of the Function Body, which must be constant

Visits: 64

Leave a Reply

Your email address will not be published. Required fields are marked *