Python Function is a piece of code or any logic that performs a specific operation. We have already seen some methods until now, and you may not notice them. For instance, print(), factorial(), round(), etc., are a few of the built-in Python functions.
Let us see the definition, declaration, syntax, and example of using functions in Python Programming language. There are two types of functions:
- We don’t have to bother about the logic inside the Library or built-in functions in this Python Programming Language. In our previous articles, We used many library methods such as print(), factorial(), round(), etc.
- User Defined: Instead of relying on built-in, it allows us to create user-defined functions. For example, if we want to implement some mathematical calculations, put them in separate methods with the correct name. Then we can call that method multiple times.
Advantages of functions in Python
- It helps to divide the large programs into small groups so that we can read the code and debug the program faster and better.
- Python Functions stop us from writing the same logic at various times. We can bind the logic in one def and then call the same repeatedly.
- Many persons can work on the same program by assigning different methods to each.
- It encourages us to call the same method with different inputs multiple times.
Python Define Functions Syntax
The syntax to define the Functions is
def FName (Parameters): Local Variable Declaration Programming Logic Executable Statement 1 …… return
- def: Keyword def is the introduction to the definition. Remember, the Python def keyword must immediately follow the Name to define it.
- FName: 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 completely depends upon the user’s requirements.
- Local Variable Declaration: Sometimes, we may need some temporary variable only for that particular method, then we can declare the local scope variables inside it. It is not compulsory, and it entirely depends upon user requirements. Remember, these variables are only available to this method, and we can’t access them outside.
- Logic: Any mathematical, code, or calculations you must implement in this Python function.
- Executable Statement: Any print statements to print some data from this particular def.
- return: This keyword is required to return something from the method. For example, returning the sum of two integers, etc.
User Defined Functions in Python implementation
To implement the user defined in a program, we have to follow a few rules, such as:
Python Function Declaration
It informs about the def name and number of arguments.
def Name (Parameters):
def Add(a, b):
How to call a Function?
Nothing but calling the original method with a valid number of arguments. For example, Add (2, 3). And remember, User defined name should exactly match the method call.
The Python function definition is where we will put all the logic, calculations, etc. Please don’t forget the return statement inside the method body; otherwise, the program not return anything. While calling it, always pass the required arguments for the block. For example,
def Adding(a, b): Sum = a + b return Sum print("After Calling:", Adding(3, 4))
After Calling: 7 >>> Adding(8, 9) 17
Program to find the Sum and Average of 3 Numbers
The user is asked to enter three numbers in this Python functions program. Then by calling the method, we calculate the Sum and Average of that three numbers.
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 Func sumAndAverage(a, b, c) sumAndAverage(10, 20, 30)
It is called a Python function declaration. If you forget this declaration, then it throws an error.
def sumAndAverage(x, y, z):
The below statements ask the user to enter 3 numbers and store the user input values in a, b, and 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 call it multiple times, first with user-specified values, then static 10, 20, and 30.
sumAndAverage(a, b, c) sumAndAverage(10, 20, 30)
When it reaches this method, it traverses to check for the sumAndAverage(). If it fails to identify the func name, it throws an error.
Within the Python function definition,
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. Please refer to print(), factorial(), and round()
Sum = x + y + z
Sum = 10 + 20 + 30 = 60
Average = Sum / 3
Average = 60 / 3 = 20
Prints 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 method arguments. We called 2 times because it helps you to understand that we can call the func n number of times.
Comments are closed.