Python Functions
Introduction
In this chapter, we will learn to use Python Functions.
Function

Creating a Function
- A function block in Python begins with a keyword def.
- A function name after that keyword "def" and parentheses "()".
- Any input parameter or arguments for a specific functionality should be placed between these parentheses.
- An optional statement, "fun_docstring" is followed by the parentheses, but it's optional.
- For accessing and invoking a function In Python, we should use a colon ":".
- Then your function or some set of code for performing a certain operation.
- Finally a return statement.
Structure
Let's have a look at the structure:
def funcname (parameters):
# “func_docstring”
[func_suite]
return [expression]
Example
- def add(a, b):
- print ("Adding : %d + %d" % (a, b))
- return a + b
- def subtract(a, b):
- print ("Subtracting : %d - %d" % (a, b))
- return a - b
- def multiply(a, b):
- print ("Multiplying : %d * %d" % (a, b))
- return a * b
- def divide(a, b):
- print ("Dividing : %d / %d" % (a, b))
- return a / b
- print ("Let's use the functions created by us")
- A = 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))
# Function: Add
# Function: Divide
Output

Calling a Function
Function Calling | Approaches

- Call By value
- The most common strategy of parameter or argument passing is call-by-value, sometimes it is also called pass-by-value. This strategy is used in several programming languages like C, C#, Java, C++, and so on.- In the call by value approach, the argument expression is evaluated first and the result of this evaluation is bound to the corresponding variable in the function. So, if the expression is a variable, a local replica of its value will be used.- Here's how it works:
ExplanationHere a replica of the list (all the values and elements in it) is used for functionality via passing a value. - Call By Reference
- In a call by reference approach, a function gets an implicit reference to the argument rather than a copy of its value. As a result, the function can modify the argument and the value of the variable in the caller's scope can be changed.- This strategy is also known as pass by reference and is used in several programming languages, such as C, C#, Java, C++, and so on.- The advantage of the call by reference is greater time and space-efficient because arguments do not need to be copied.- Here's a sample functionality:
ExplanationIn this approach, it works slightly differently. Here there are two locations and for both of the locations, only a reference is being used instead of the replica itself.
Function Calling | Arguments

- Default Arguments
As the name suggests, a default argument always assumes a default value in case there is no value provided in the function call for a specific argument.ExampleOutput
- def empinfo(name = "ABC", designation):
- print ("Name : ", name)
- print ("Designation : ", designation)
- return;
- empinfo( designation = "Dev" )
Now let us see some changes in the code and call the function again for a different pair or a different value:Output- def empinfo(name = "XYZ", designation):
- print ("Name : ", name)
- print ("Designation : ", designation)
- return;
- empinfo( name = "ABC", designation = "Dev" )
- empinfo( designation = "Dev" )

- Required Arguments
Required arguments are those that are passed to a function in a proper positional/hierarchical order. In Python, the number of arguments in the function call should be exactly the same as the function definition.ExampleOutput
- def callme( str ):
- # "This prints a passed string into this function"
- print (str)
- return;
- callme()
ExplanationAs I explained in the preceding paragraph, a required argument needs to pass one argument in the callme function and we didn't do that. That's why the snippet is showing an error.But if you pass an argument then it will produce an output for sure. - Keyword Arguments
Keyword arguments are somehow similar to the function calls. Whenever you use keyword arguments in a function call, it is identified by the parameter name directly.There are many advantages to the use of this approach, these are:
-
- it allows you to skip the arguments.
- Even you can place the arguments out of order.- You can also make keyword calls (using function name).Output- def callme( str ):
- print (str)
- return;
- callme( str = "Calling that function");
ExplanationIf you haven't yet gotten the theme then don't worry. Let's explore it with a similar type of example as we used for the default try arguments, to provide you a clear view.Output- def empinfo(name , designation):
- print ("Name : ", name)
- print ("Designation : ", designation)
- return;
- empinfo( name = "ABC", designation = "Dev" )
(I hope now it will be clear to you guys.) - Variable Length Arguments
All the three above specified arguments are enough to perform any operation, but still, you may need to process a function for more arguments than you specified. For that, here comes the concept of Variable Length Arguments.ExampleOutput
- def callme( arg, *vartuple ):
- print ("India??")
- print (arg)
- for var in vartuple:
- print (var)
- return;
- callme( "Democracy", "or", "Gerontocracy")

Conclusion
In the next chapter, we will learn to use Python List.
Author
Abhishek Jaiswal
97
19.8k
9.7m