Introduction to Functions in Python Programming
Functions in Python: Function is the most important topic of any programming language. It is nothing but just a block that contains a set of codes or instructions or we can say programs. It is used to perform specific tasks when needed.
Why we use Functions: Sometimes when we write a program, we use the same code again and again and what we do is we write the same code multiple times which increases the line of codes as well as the code complexity also. To overcome this problem we use functions.
Syntax: In python “def” keyword is used to define a function. Syntax is given below:
# Functions without parameter def functionName(): #statement-1 #statement-2 #function with one parameter def functionName(parameter): #statement-1 #statement-2 #function with two parameters def functionName(parameter-1,parameter-2): #statement-1 #statement-2
Note: Parameters passed to functions are nothing but just the value passed externally to functions. We will discuss it later in examples.
Example 1: Function example:
#Example Functions # Function_1 def add(): print("Addition Function Called") # Function_2 def sub(): print("Subtraction Function called") # Function_3 def hello(): print("Hello World") # Calling function_2 sub(); # Calling function_3 hello(); # Calling function_1 add();
Output:
# Subtraction Function called # Hello World # Addition Function Called
Explanation:
- Here in the above example, I have defined 3 functions just to visualize how functions concept work. We defined three functions using the “def” keyword followed by the function name.
- As I said before in functions we define statements inside the body of the function. These statements act as the functionality of function.
- Let me make you more clear: see function-1 (above example) def add() is the function declared and print(“Addition Function Called”) after : is the statements inside function.
- That is the complete function definition. Our function will not show any result before we call it. FunctionName() followed by semicolon(;) ie add(); is used to call.
- If we call the function three times then it will return the result three times.
See the below example:
#Example Function without parameter # Function def add(): print("Addition Function Called") # Calling-1 add(); # Calling-2 add(); # Calling-3 add();
Output:
# Addition Function Called # Addition Function Called # Addition Function Called
Benefits of using Functions
- Write once use multiple: Sometimes in the program, we write some code and then later we need that code again to perform the same task. What we do is we write the same code again. This is not the best practice to go through. We use the concept of functions in python. We define the function once and call it multiple times when required.
- Easy Readability: Using the concept of functions, we do not write the same code again and again. Hence our program became structured and readable. Hence it improves the readability of our program/code.
- Reduce Execution Time: Using functions our lines of code is reduced and hence it results in the fast execution of our program.
Non-Parameterized and Parameterized Functions
Functions are either be parameterized or non-parameterized.
(i). Non_Parameterized Function: Normal and simple functions are non-parameterized functions. The functions we defined in the above examples are non-parameterized functions.
Syntax:
#Non-Parameterized Function def fun_name(): #Statement #Function with no parameter
(ii). Parameterized Function: Functions that get values externally or at the calling time, these types of functions are called functions with the parameter or parameterized function. Parameters are one or more than one also. See below given example:
# Parameterized Function # Function with one parameter def fun_name1(x): print(x) #Statement # Function with two parameters def fun_name2(x,y): print(x+y) #Statement # Function with three parameters def fun_name3(x,y,z): print(x+y+z) #Statement # Calling fun_name1(2) fun_name2(2,3) fun_name3(2,3,4)
Output:
#2 #5 #9
Arguments: Value passed to function at the calling time are pronounce as arguments.
Default Arguments in Python Programming
Default arguments are the concept used in parameterized functions only. It is used to avoid errors at the time of calling function and passing value as an argument.
Let’s suppose we defined function with two parameters, but at the time of calling function, it is necessary to pass two arguments also. If we do not pass the required number of arguments, then our program returns an error. To solve this type of problem we use default arguments.
Please follow the below given example below to understand more clearly.
# Function with two parameters def fun_name(x,y=3): print(x+y) #Statement # Calling fun_name(2)
Output:
#5