What are the types of Functions in Python: Types, Arguments, and Variable Scope.

 

What are the types of Functions in Python:  Types, Arguments, and Variable Scope.


Functions in Python


A function is a specific block of code that performs a specific and similar task. Just as the electric fan that rotates above our head does a specific job, the tube light that burns is also doing a specific job. That is, they are always a function that has to be driven by electricity.


Types of function


We have already used some functions, like print() function to display the result on the console.  Functions are of two types –


 *Built-in function.


 *User defined function.


The print() function we've used so far is a "built-in function" that comes with the language. But in some cases not all work is done with this 'built-in function'.


Then there is a need to create some functions themselves, and when the user creates a function, it is called a "User defined function".


In this section we will discuss user defined functions, learn how to create functions.


Different parts of a function


 A function basically has four parts-


-Function name.


-Argument or parameter of the function.


-Function body.


-Return type.


Different parts of function are recognized with an example –


 #Function Definition

 def multiply_function(x,y):

     z=x*y

    return z


print(multiply_function(5,10)) #calling a function


 Result-

 50


The above program shows a complete function. Writing a function in Python starts with the "def" keyword, followed by the function name; "multiply_function" This is the name of the function.


The name of the function can be anything, but the more meaningful the better, since in this function we do multiplication, we named it "multiply_function" so that it is easy to understand what it does by looking at the name.


Note, the use of semicolon "(:)" is mandatory here. Notice that after the function name, "x" and "y" variables are used in brackets, these are the parameters of the function and these variables act like local variables of the function.


After this, the "z=x*y" statement is used, which is the body of the function. A return statement is used at the end. The return keyword is used to specify what the function will return.


We want to return the result (z) of the multiplication in this function, so "return z" is used here.


This function has not been called yet, so it will not do anything yet i.e. running the program will not display any results. The function needs to be called for the result.


To call a function, call it with the function name and arguments. In the 4th line I put the two tasks together, call the function and display the result with the "print() function".


This is what the main function call does –


 multiply_function(5,10)


Here two arguments '5' and '10' are used inside the function name. These arguments will be passed to the main function and will be substituted as the values ​​of "x" and "y" respectively.


That is, we can calculate the multiplication of any number using this function.No need to write new code again and again, just call function with arguments.


Types of arguments


Python generally requires arguments to be used if a function has parameters.


Python also has three types of arguments –


-Keyword argument.


-Default argument.


-Variable length argument.


Keyword argument


In the previous example, we have seen that by passing two arguments, we get its product. Exact value before and after is no problem, but what happens if this function is written for division?


If the value is reversed earlier, the quotient will differ greatly. To solve this problem, the keyword arguments is used to specify the value of the variable inside the argument.


Let's see this with an example –


 def multiply_function(x,y):

z=x/y

return z


 print(multiply_function(5,10)) #normal function call


print(multiply_function(y=5,x=10)) #function call using keyword argument


 Result-

 0.5 

 2.0


Hope you understand the difference correctly. In general, if we call the function, the value of the variable is assigned to the sequence.


That is, in the 4th line the value of "x" is calculated as '5' and the value of "y" is calculated as '10';


But in line 5, since we have assigned the value to the keyword, the value has also changed.


Default argument


So far we have learned that to call a function, if it has any parameters, it must be called with arguments, otherwise an error will be thrown.


But now let's see how to call a function without arguments despite having parameters. A call without an argument is called a default argument.


For the default argument, the function definition must provide this default value.  Here is an example to explain the point better –


def multiply_function(x=10,y=5):

z=x/y

return z


 print(multiply_function()) 

#function call using default argument


 print(multiply_function(100,5)) #normal function call


Result-

2.0

20.0


In the 4th line we can see that the function is called without any arguments.Basically in the 1st line in the function definition we have given some default value which is acting as the argument here and showing '2' as the result.


In line 5 we called a simple function, passing '100' and '5' as arguments, in this case the result returned '20'.


That is, in the case of default argument, without giving any value, the default value will be taken as an argument and if some value is given to the argument, it will prevail.


Variable length argument


Just by looking at its name, you can guess what we are talking about. Many situations can arise when we don't know how many arguments to send or the arguments are difficult to specify.Python has variable length arguments to solve this problem.


Let's see the point through an example.


def multiply_function(*values):

z=values[0]/values[1]

return z


 print(multiply_function(100,5))

#function call


Result-


20.0


The function definition has one parameter but is passing two values ​​as arguments.  When the value goes, it goes as a list.


Note here that the parameters of the function definition must be preceded by a '*' symbol.


A value can also be sent via a 'key' using this method. In that case the 'key' and its value have to be given as before.


Here is an example –


def multiply_function(**values):

z=values['x']/values['y']

return z


 print(multiply_function(x=100,y=5)) #function call


Result-


20.0


Here "x" and "y" act like a 'key'.


Note here that the variable in the function parameter must be preceded by a '**' sign.  It basically works as a dictionary.


Read more: Python data type,Loops,String,Conditional statement

Variable Scope


The variables we've used so far have had very limited scope, so we haven't bothered with variables. But if the need to write such a program is very large, then caution is necessary in the use of variables.  


Variables can be divided into two categories depending on their scope-


-Local Variable


-Global variable


Local Variable


When the scope of a variable is limited to a specific code block or function, it is called a local variable.


What are the types of Functions in Python:  Types, Arguments, and Variable Scope.


 Global variable


When a variable has scope throughout the program, it is called a global variable.


Now we have seen an example related to –


1. x=100

2. def division_globalvar_function(value):

3. z=x/value #Division using global variable x

4. return z


5. def division_localvar_function(value):

6. x=20

7. z=x/value #Division using local variable x

8. return z


9.print(division_globalvar_function(5))#function call


10.print(division_localvar_function(5)) #function call


Result –


20.0

4.0


In the 1st line of the above program, "x" is acting as a global variable. In the 3rd line, "x" is used.


That is, the value of "x" exists throughout the program, so it is a global variable. When this function is called on line 9, the result is 20.


But later in the 6th line "x" is assigned the value 20. When "x" is working inside this function it will first be used as a local variable.


Here both variables have the same "x" name, but since there is a conflict, the local variable will take precedence. In this case, when it is called on line 10, the result is 4.0


Note that global variables have more scope, but local variables have more power in terms of power.


Conclusion:


Functions in Python are essential for organizing and reusing code. The function name should be chosen wisely to reflect its purpose and make the code more readable.


Arguments are used to pass values into the function, and the function body contains the code that performs the desired operations.


In summary, Functions in Python provide modularity, reusability, and code organization.This allow us to break down complex tasks into smaller, manageable units of code.


By understanding the different types of arguments and variable scopes, we can write more flexible and efficient functions. 


Utilizing functions effectively enhances code readability, maintainability, and promotes good programming practices.


FAQ


Q: How can functions in Python enhance code organization and reusability?


Functions in Python provide modularity, allowing code to be broken down into smaller, manageable units. This promotes code reusability as functions can be called multiple times from different parts of the code.


Q: What should be considered when choosing a function name in Python?


A function name should be chosen wisely to reflect its purpose and make the code more readable.It's recommended to use descriptive names that accurately convey what the function does.


Q:What is the purpose of arguments in a Python function?


Arguments are used to pass values into a function. They allow the function to work with different input values and perform the desired operations based on those values.


Q: How can variable scopes impact the use of variables in Python functions?


Python variables can have local or global scope.Local variables are limited to the specific code block or function where they are defined, while global variables have scope throughout the entire program.


Understanding variable scopes is important to avoid conflicts and ensure the correct usage of variables.


Q:What are the advantages of using default arguments in Python functions?


Default arguments allow functions to be called without providing values for all parameters.


They provide flexibility by assigning default values to parameters, which are used when no other value is specified.


This avoids errors and simplifies function calls in certain scenarios.


Q:What are some best practices for using functions in Python?


Some best practices for using functions include using descriptive function names, providing clear documentation and comments, keeping functions focused on a specific task, and avoiding excessive side effects or dependencies within functions.


These practices contribute to code clarity and maintainability.

Post a Comment

0 Comments