This article is a step-by-step continuation from the previous 12 chapters of Python.
This is the 13th part of this article series about Python. In this article series you will learn Python step-by-step and in an easier way.Getting ThemeFor getting the theme of Python, kindly go through my previous articles:
FunctionA function in programming terms can be defined as “a block of reusable, managed code block that is used to perform any certain single operation.”Functions provide better functioning for your application and a high degree of code reusability. We can also call it a machine that gives desired outputs depending on our input. Something like the following: Python contains several types of built-in functions such as print(), del, len and so on. These types of functions are called built-in functions, but still, like several other programming languages, they provide complete freedom to create your own functions based on your requirements. These types of functions are called user-defined functions. Creating a Function As I said earlier, you can create your own built-in functions. So let's have a look at how to create a function.The following are the set of rules that you need to follow when creating a function:
StructureLet's have a look at the structure:
Def funcname (parameters):“func_docstring”Func_suitereturn [expression]
ExampleCreating several basic functions.
# Function: Adddef add(a, b):print ("Adding : %d + %d" % (a, b))return a + b# Function: Subtractdef subtract(a, b):print ("Subtracting : %d - %d" % (a, b))return a - b# Function: Multiplydef multiply(a, b):print ("Multiplying : %d * %d" % (a, b))return a * b# Function: Dividedef divide(a, b):print ("Dividing : %d / %d" % (a, b))return a / bprint ("Let's use the functions created by us")# Calling FunctionsA = add(5, 5)B = subtract(5, 6)C = multiply(6, 5)D = divide(5, 5)print ("A : %d | B : %d | C : %d | D : %d" % (A, B, C, D))
OutputCalling a FunctionCreating a function is just 50% of the entire work, moreover for performing certain operations you need to call that function later in your code snippet. So let's have a look at how it can be done in Python.Once the basic block of function is formed, here are the approaches that you need to use when calling a function in Python.Function Calling | ApproachesHere are some approaches:
Moto“Keep calm and code Python”. I tried to make it an interesting and interactive article and wish you guys like that, meanwhile if you have any suggestions then your welcome.Until the next part, keep sharing!
Pro WPF: Windows Presentation Foundation in .NET 3.0