Updated April 13, 2023
Introduction of Python Function
Functions are the basic building blocks in programming languages. Suppose you are working on big projects, and there might be a possibility that you want to perform the same operation, again and again; in that case, we create functions and write that particular code inside that function.
Now we can use this function as many times as we want and perform that particular operation. For Example: in an e-commerce website, you want to do a payment for a product you purchased; in that case, we can write a payment code in a function. This function will be used again and again for all purchasing. Let’s look at a few stages on how to call a function in Python.
How to Create Functions?
There are two parts of the function, first, define the function and second calling a function. The functions which we make manually are called User-defined Functions.
Function declaration without argument
Input:
def makePayment():
print("hello")
print("hi")
print("abcdefg")
Output:
This is how we declare a function, def is the keyword used for defining and “makePayment” is the function name and colon at the end; now, you can write multiple lines into your functions. This function does not have any arguments; passing arguments into functions is not mandatory. The colon indicates that there is some piece of code inside this function.
def makePayment(argument1, argument2, ...):
A function declaration with arguments. Arguments are the values or data that will be used in the code written under the functions.
How do Functions work in Python?
Functions are the piece of code that is not executed automatically until you won’t call it. To call the function, just write the name of the function. Whenever a function is executed, a new symbol table is created internally in the memory. All the arguments passed into function stores the values into a local symbol table. A reference variable first looks into the local symbol table; then it tries to find the function defined inside a function, then it tries to find the global symbol table and then at last inbuilt names. A global variable cannot be assigned inside the function because it will be not accessible throughout the system, but you reference it. Whenever a new function is called inside another function new symbol table is created.
Whenever you define a variable inside a function that can be accessed inside the function only, you cannot access that variable out the function. If the variable is defined outside, the function can be used anywhere or into many functions.
makePayment()
Calling function without argument.
makePayment(arg1, arg2, ...)
Calling function with arguments.
If you passed the argument while defining the function, you have to pass the parameter while calling the functions; otherwise, it will result in an error. The number of parameters while defining and calling should be the same. If you perform any calculation or have written business login into a function, you also have to return the final value from the function.
We can also create a function with arguments but with optional arguments and defining default values to those arguments.
def exampleFunction(a, b=1, c="abcd"): print(c)
print(a+b)
Now we can call this function in two ways.
exampleFunction(5)
In this case, we are passing a value for argument “a,” an argument “b”, and “c” will take the default argument.
exampleFunction(1,2,"efgh")
In this case, we have passed are three input arguments.
Logics written in functions are easy to understand and easy to debug them; you know which function is not working properly or generating errors, so you don’t need to go to throughout the entire code of the page; you can just debug that functions.
Variable Length Arguments: Let suppose we have a function, but we don’t know how many arguments we need to while calling that function; it might change every time. In that case, we can define the variable-length argument function.
def exampleFunction(*args):
for value in args:
print(value) #exampleFunction(a,b,c)
This function will take n number of arguments as we have * in the arguments, and it will print all the incoming arguments.
Examples of Python Function
Following are the examples are given below:
Example #1
Function to print the message.
Code:
def printMessage():
print("Hello")
print("How are you")
printMessage()
Output:
This is a basic user-defined function without any argument. So while calling, we haven’t passed any argument.
Example #2
Let’s make a function that will perform basic calculations.
Code:
def peformCalcuation(a,b):
print('Addition => ', a+b)
print('Subtraction => ', a-b)
print('Multiplication => ', a*b)
print('Division => ', a/b)
peformCalcuation(10,2)
Output:
In this example, we have passed two-argument a,b, and printed their result. So while calling this function, we have to pass the exact number of arguments.
Example #3
Let’s make a function that will find the odd or even, and we return the final result.
Code:
def oddEven(a):
if a%2 == 0:
return True
else:
return False
result = oddEven(15)
print(result)
Here we have created the function, but this function will print any results. We are just returning the result. So now, while calling the function, we have to capture the result of the function into another variable.
Now the desired will be printed.
Output:
We also have an anonymous function in python known as the Lambda function. They are a one-line function and are not defined using the “def” keyword, and they only return expression. We make use of the “lambda” keyword to define these functions.
These kinds of functions don’t have any specific name, but we can assign this function to any variable.
Example #4
Code:
lambda args: expression
sum = (lambda a,b:a+b)
print(sum(1,2))
Output:
These functions are useful when we need to perform a single line operation.
Conclusion – How to Call a Function in Python
Programming application making use of functions creates a robust application and easier to manage code and code reusability. Try to keep the length of functions limited, and if your function is going big, then divide them into smaller functions. If you are passing any values to a function, always keep default values to prevent errors.
Recommended Articles
We hope that this EDUCBA information on “How to Call a Function in Python?” was beneficial to you. You can view EDUCBA’s recommended articles for more information.