In Python, one can say that functions are part of the programming which enhances its power. Hence, programming tasks can be decomposed into subtasks and handled independently making scripts more generic and maintainable. It is important to learn how to define and call functions before writing an organized Python application. In this article, we will handle total functions, how they are formed and scope in general as a fundamental concept for the understanding of how each variable operates in different areas of your application.
In this case structure, function_name means the name of the function created and parameter1, parameter2 and others are the parameters passed to the function. The return command is not necessary but when it is used, in this case, specifies what value is sent back to the segment of the program that invoked that function.
Let's say you configured a spark job to always go to the data repository, and you created the aggregate_job(spark_config: lage_huge). Back to the calling. The action performed against a function is described as being a call to that function. Calling a function includes mentioning its name, along with providing its parameters, if there are any.
If a function does not accept any arguments, it is acceptable to mention it without parentheses.
Here is an example of a function assuming parameters a and b and adding a to b:
In this code block `add_numbers` is the name of the function which adds two numbers. `a` and `b` are the two numbers that are to be added together. And when the function gets called, the sum of `a and b' are set to a variable `result` and are printed.
There are basically two classes of scope in Python:
1. Global Scope This is the set of variables that have been defined outside of any function and in other words, they can be utilized in any part of the program.
2. Local Scope This is a scope that comprises variables which have been defined within a function and can only be utilized within that function as they cannot be accessed from outside of that function.
For example:
In this scenario, " x " which is defined outside the function print_global( ) is a global variable. It can be used by the function print_global( ) and also by any other part of the program.
For instance:
In this case, sum_result is defined within the add_numbers function which is not accessible outside as it was not defined in the global context Once you print the result, there would be an error.
Examples of modifiers from global variables being accessed within functions.
If you need to bring about a change in a variable that is accessible in the global space then this can only be done using the global syntax. Without this modifier in python programming all variables inside the block are locally accessible only even though there is one having a similar name.
This is how the global variable can get changed in the function of the below example :
Here the result of the print function is set to be 20. This change was able to happen due to setting a modifier to global in this example .
Python would still continue to treat it as a local variable if there was no global present.
Furthermore, grasping the concept of scope can help you improve the performance of the code you write. For instance, when a variable is required only within a particular function, it is possible to declare it as a local variable which decreases the probability of name collision and improves the efficiency of the program by decreasing the life span and the amount of memory allocated to the variable.
What Is Function?
To put it simply, a function is a sequential set of codes combined to accomplish a specific purpose in Python. When a function is created it has an explicit map and hence when it is invoked multiple times the code is easier to read because it's executed once leaving the original code intact. As a result, pieces of code can be reused, allowing programmers to avoid repetitions all through the program. Furthermore, this makes management easier because each procedure handles distinct jobs.A function is defined as one that receives an input (also known as a parameter or argument) and performs a certain computation with it which may even produce an output called a return value. The great thing about functions is that the same function can be called repeatedly, with different sets of inputs, thus making the program more interesting.
Creating a Function
To create a function in Python is quite easy. Type the def keyword, which means you want to define a function, then write the desired name followed by a pair of brackets. Inside the brackets, you can specify any parameters that the function will accept. The indentation in a program in Python is critical, for that is where the body of the function is indented below the main delta. This is how a function in python is constructed as a rule:Function Structure
def function_name(parameter1, parameter2, …):
Code block that performs an operation
On the parameters
return result
}
Calling a Function
Defining a configurationLet's say you configured a spark job to always go to the data repository, and you created the aggregate_job(spark_config: lage_huge). Back to the calling. The action performed against a function is described as being a call to that function. Calling a function includes mentioning its name, along with providing its parameters, if there are any.
If a function does not accept any arguments, it is acceptable to mention it without parentheses.
Here is an example of a function assuming parameters a and b and adding a to b:
Addition Function Example
def add_numbers(a, b):
return a + b This is called the function.
Invoking the function with an argument
result= add_numbers(5, 3)
print(f'the result is: {result}') The output is logically expected to be equal to 8
The Scope of A Function
Functions are intricately linked with the idea of scope. Scope of a particular variable is defined as the area of the program within which that particular variable is available for use. In python, scope of a variable controls which part of the program is going to use that variable and in which part of the program it will be changed.There are basically two classes of scope in Python:
1. Global Scope This is the set of variables that have been defined outside of any function and in other words, they can be utilized in any part of the program.
2. Local Scope This is a scope that comprises variables which have been defined within a function and can only be utilized within that function as they cannot be accessed from outside of that function.
Global Variables
A global variable is simply defined as a variable that has been defined outside any function. Such variables can be referenced and changed by any program segment including functions. For instance, if a variable is defined within a function, it is local to that function. It is not possible to reference such a variable from outside the function unless a value of that local variable was returned.For example:
Global Variable Example
x = 10 Variable which is Global
def print_global():
print(x) Global variable is being Accessed Inside A Function
print_global() Result will be 10
Local Variables
A local variable is a variable that has been defined within a function and can be used only in that function. It remains unreachable from other parts of the program outside the function.For instance:
Local Variable Example
def add_two_numbers(a: int, b: int) -> int:
sum_result: int = a + b
return sum_result
Examples of modifiers from global variables being accessed within functions.
If you need to bring about a change in a variable that is accessible in the global space then this can only be done using the global syntax. Without this modifier in python programming all variables inside the block are locally accessible only even though there is one having a similar name.
This is how the global variable can get changed in the function of the below example :
Global Variable Modification Example
x = 10
def global_x_addition():
global x
x = 20
global_x_addition()
print(x)
Python would still continue to treat it as a local variable if there was no global present.
Significance of Scope
The reason why it is important to grasp the concept of scope is so that you are able to control the visibility of the variables of the program. This reduces the likelihood of globals being modified undesirably and provides better control of data as it is passed among components of the program. For example, in the case of functions where the use of local variables suffices, such practices can be helpful in averting the unplanned alteration of global variables making the behavior of a program more deterministic and easier to troubleshoot and test.Furthermore, grasping the concept of scope can help you improve the performance of the code you write. For instance, when a variable is required only within a particular function, it is possible to declare it as a local variable which decreases the probability of name collision and improves the efficiency of the program by decreasing the life span and the amount of memory allocated to the variable.
Article
Be the first comment