Functions in Python - 7 things you should know.
In simple terms, a function is a device that groups a set of statements so they can be run more than once in a program—a packaged procedure invoked by name. Functions also can compute a result value and let us specify parameters that serve as function inputs and may differ each time the code is run.
More fundamentally, functions are the alternative to programming by cutting and pasting—rather than having multiple redundant copies of an operation’s code, we can factor it into a single function. In so doing, we reduce our future work radically: if the operation must be changed later, we have only one copy to update in the function, not many scattered throughout the program. Functions are also the most basic program structure Python provides for maximizing code reuse, and lead us to the larger notions of program design. As we’ll see, functions let us split complex systems into manageable parts. so two primary benefits -
1: Code reuse
Python functions are the simplest way to package logic you may wish to use in more than one place and more than one time.Functions allow us to group and generalize code to be used arbitrarily many times later. Because
they allow us to code an operation in a single place and use it in many places, Python functions are the most basic factoring tool in the language: they allow us to reduce code redundancy in our programs, and thereby reduce maintenance effort.
2: Procedural decomposition
Functions also provide a tool for splitting systems into pieces that have well-defined roles.It’s easier to implement the smaller tasks in isolation than it is to implement the entire process at once. In general, functions are about procedure—how to do something, rather than what you’re doing it to.
Lets dive a little deeper understanding functions, by understanding below 7 concepts.
1: " def " is executable code.
Python functions are written with a new statement, the def.def is an executable statement— your function does not exist until Python reaches and runs the def. In fact, it’s legal (and even occasionally useful) to nest def statements inside if statements, while loops, and even other defs. In typical operation, def statements are coded in module files and are naturally run to generate functions when the module file they reside in is first imported.
2: def creates an object and assigns it to a name.
When Python reaches and runs a def statement, it generates a new function object and assigns it to the function’s name. As with all assignments, the function name becomes a reference to the function object. def does not execute anything else, it just creates a function object and assigns a name to it. Its only when the function is called from a program - is it executed. I have covered how objects come into existence in detail here.
3: lambda creates an object but returns it as a result.
Functions may also be created with the lambda expression, a feature that allows us to in-line function definitions in places where a def statement won’t work syntactically.
# Program to filter out only the even items from a list >>> my_list = [1, 5, 4, 6, 8, 11, 3, 12] >>> new_list = list(filter(lambda x: (x%2 == 0) , my_list)) >>> print(new_list) [4, 6, 8, 12]
4: return sends a result object back to the caller.
When a function is called, the caller stops until the function finishes its work and returns control to the caller. Functions that compute a value send it back to the caller with a return statement; the returned value becomes the result of the function call. A return without a value simply returns to the caller (and sends back None, the default result).
5: yield sends a result object back to the caller, but remembers where it left off.
Functions known as generators may also use the yield statement to send back a value and suspend their state such that they may be resumed later, to produce a series of results over time. I talk yeild and generators in quite some details here.
6: global declares module-level variables that are to be assigned.
Any name defined inside a function is available only inside the function. Once the function exits, these names cease to exist. I speak about this topic in detail here.
7: Arguments, return values, and variables are not declared.
As with everything in Python, there are no type constraints on functions. In fact, nothing about a function needs to be declared ahead of time: you can pass in arguments of any type, return any kind of object, and so on. As one consequence, a single function can often be applied to a variety of object types—any objects that sport a compatible interface (methods and expressions) will do, regardless of their specific types. This is called polymorphism which means methods adapt what they do , depending on objects so x*3 can mean 10 if x = 7 integer or it can mean "brkbrkbrk" if x is "brk" string. I have covered polymorphism in detail here.
If you are intuitively clear on these 7 ideas - you should be very clear on what functions are.
If you liked this article please do share ahead using social icons below.