Block of code or some logic, which performs specific operation is called as Function. We already seen some functions until now, you may not noticed them. For instance, print(), factorial(), round() etc are some of the built-in functions in Python programming language.
In this article we show you the Function definition, declaration, syntax, and example of using functions in Python.
Advantages of a functions in Python
- It helps to divide the large programs into small groups so that, we can understand the code, and debug the program quicker and better.
- Python Functions prevents us from writing same logic multiple times. We can wrap the logic in one function, and then call the same over and over.
- Multiple persons can work on same program by assigning different functions to each of them.
- Helps us to call the same function with different inputs over multiple times.
Python Functions Syntax
The basic syntax of the Functions in Python Programming is as shown below:
def Function_Name (Parameters): Local Variable Declaration Programming Logic Executable Statement 1 …… return
- def: Keyword def is the introduction to function definition. Remember, def keyword must immediately followed by Function_Name
- Function_Name: It can be any name you wish to give other than the system reserved keywords.
- Parameters: Every python function accepts 0 or more parameters, it’s completely depends upon the user requirements.
- Local Variable Declaration: Sometimes, we may need some temporary variable which are required only for that particular function then we can declare them inside the function. It is not mandatory, completely depends upon user requirements. Remember, these variables are available to this particular function only, and we can’t access them outside the function.
- Logic: Any mathematical or any type of code or calculations you want to implement in this particular function.
- Executable Statement: Any print statements to print some data from this particular function.
- return: This keyword is required to return something from the function. For example, returning the Sum of two integers etc.
Types of Functions in Python
There are two types of functions in Python Programming language:
- Library Functions: Built-in functions in Python Programming Language are called as Library function. We don’t have to bother about the logic inside the Library functions. In our previous articles, We used many library functions such as print(), factorial(), round() etc.
- User Defined Functions: Instead of relying on built-in functions, Python programming language allows us to create our own functions called as user defined functions. For example, if we want to perform some mathematical calculations then we can place them in separate function with proper function name and then we can call that function multiple time.
User Defined Functions in Python implementation
To implement the user defined functions in Python program we have follow few rules such as:
Python Function Declaration
It will inform the compiler about the function name, and number of arguments.
def Function_Name (Parameters):
def Add(a, b):
Calling the Function in Python
Nothing but calling the original function with valid number of arguments. For example, Add (2, 3). And remember, User defined function name should exactly match with the calling function.
Python Function Definition
This is the place where we are going to put all the logic’s, calculations etc. For example,
def Adding(a, b): Sum = a + b return Sum print("After Calling the Function:", Adding(3, 4))
NOTE: Please don’t forget the return keyword, otherwise the program will not return anything.
Find Sum and Average of 3 Numbers using Functions in Python
In this Python functions program user is asked to enter three numbers and then by calling the function we will calculate the Sum and Average of that three numbers.
# Python Functions Example def sumAndAverage(x, y, z): Sum = x + y + z Average = Sum/3 print("\n %d is the Total Sum of three Numbers." %Sum) print("\n %d is the Average of three Numbers.\n" %Average) # Allows User to enter three values a = int(input("\nPlease Enter the First Value. a = ")) b = int(input("\nPlease Enter the Second Value. b = ")) c = int(input("\nPlease Enter the Third Value. c = ")) # Calling the Function sumAndAverage(a, b, c) sumAndAverage(10, 20, 30)
This is called as function declaration. If you forget this function declaration then the compiler will throw an error.
def sumAndAverage(x, y, z):
Below statements will ask the user to enter 3 numbers, and store the user input values in a, b, c variables
a = int(input("\nPlease Enter the First Value. a = ")) b = int(input("\nPlease Enter the Second Value. b = ")) c = int(input("\nPlease Enter the Third Value. c = "))
In the next line, we are calling the function multiple times. First with user specified values, and then static 10, 20, 30
sumAndAverage(a, b, c) sumAndAverage(10, 20, 30)
When the compiler reaches this function then it will traverse to check for the sumAndAverage() function. If the function fails to identify the function name then it will throw error.
Within the function,
def sumAndAverage(x, y, z): Sum = x + y + z Average = Sum/3
we declared 2 local variables Sum and Average. In the next line, we calculated the sum and average of three numbers using Assignment Operators
Sum = x + y + z
Sum = 10 + 20 + 30 = 60
Average = Sum / 3
Average = 60 / 3 = 20
Below print statements are used to print the sum and average to the output
print("\n %d is the Total Sum of three Numbers." %Sum) print("\n %d is the Average of three Numbers.\n" %Average)
In the next line, We called Average () one more time, this time we passed local variables as function arguments. We called 2 times because it will help you to understand that, we can call the function n number of times.
Thank You for Visiting Our Blog