How to Write and Call a Python Function

In this article will talk about How to Write and Call a Python Function and We’ll go through python functions in further depth, We’ll write and call a custom Python function. If you didn’t see our last article Python Built-In Functions with Syntax and Examples know more about python.

The Python functions, as we already know, provide a systematic approach to problem-solving strategies by breaking a given problem into numerous sub-problems, finding their solutions, and combining the solutions of individual problems to solve the original problem.

Syntax for Write and Call a Function

The following is the general syntax for defining a function:

def function_name(parameters):
    statements

It begins with the keyword def and then the function name. Keep in mind that the function name and the Python keyword should not be the same. Within the parentheses, you may define the parameters separated by commas.

The parenthesis can also be left blank. The statements within the function must be indent four spaces from the left column, starting at the left margin. This is refer to as indentation. According to Google’s coding rules, we utilized four spaces for indentation.

Python Keywords

These are the Python keywords, don’t use them as the function names.

False, class, finally, is, return, None, continue, for, lambda, try, True, def, from, nonlocal, while, and, del, global, not, with, as, if, or, yield, assert, else, import, pass, break, except, in, raise, elif

You may now work on the example by creating a Python file (name.py) in your system, using Jupyter Notebook, Google Colab, any online compiler, or Python IDLE.

def main():
    print("Ranjeet")
main()

We defined the main function here, and we print a statement within its body. After that, all you have to do is call the function. One thing to keep in mind is that you can’t call a function from within its body.

Output:

Ranjeet

Let’s look at another example. We’ll look at how to utilize arguments within a Python function in this example:

def addition(a,b):
    print(a+b)
    
addition(3,8)

Output:

11

We added two parameters to the Python code in the preceding example. The variables are a and b. We just print the total of both arguments within the function’s body.

After that, call the function using the arguments’ values. 3 is allocate to a, while 8 is assign to b in the example above. As a result, the program’s output is 11.

Python return statement (Write and Call Function)

The function call is terminate by a return statement, which “returns” the result to the caller.

Let’s take a look at the previous problem with Python’s return statement. Using the Python return statement, we’ll show the sum of two integers.

def addition(a,b):
    return (a+b)
    
x = addition(3,7)
print(x)

Output:

10

The total of both arguments is returned in the program above. The function is then called with argument values and a variable x is assigned to it. Then just print the result. Isn’t it nice to work with Python functions?

Area of Rectangle

Let’s look at another case. We’ll work with the area of a rectangle in this example. The area’s length, breadth, and value should all be numerical.

def Rectangle(l,b):
    area = l*b
    return area
def main():
    length = int(input("Enter integer value of length: "))
    breadth = int(input("Enter integer value of breadth: "))
    areaOfRectangle = Rectangle(length, breadth)
    print("Area of the rectangle is: ",areaOfRectangle)
    
main()

Because the main() function is invoked globally, it runs first. Numeric length and breadth are specified. To get the user’s length and breadth values, we utilized the input() method.

The Rectangle() method is then invoked. We just multiply both arguments in the Rectangle() method. Because we use the formula length * breadth to compute the area of a rectangle. Return the region to its original state. This region is saved in the areaOfRectangle variable. Simply print areaOfRectangle now.

Fruitful Functions vs Void Functions

  • A fruitful function, for example, the area of a rectangle, is a function that returns a value.
  • A void function is a function that does not return a value.

If you want to learn more about python then visit the official documentation of python.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]

If you want to learn more about the python programming language then click the link to see our last article Python Built-In Functions with Syntax and Examples

Leave a Comment