Python Functions
What is a Function?
A function stands as a fundamental concept in programming, representing self-contained modules of code that fulfill specific tasks. Functionality-wise, it's akin to a mathematical concept grouping operations into a coherent identity, simplifying complex expressions. Functions are employed to compartmentalize different program functionalities, enabling their combination for intricate tasks, with most programming languages offering pre-existing functions stored in libraries. This approach minimizes repetition, streamlines code, enhances usability, and facilitates maintenance. Every function involves input and output, with the function's instructions dictating how output is generated from input. Custom functions can also be developed for specialized tasks.
Python Functions
To define a function, the "def" keyword is employed, followed by the function's name and a pair of parentheses encompassing any parameters the function may require, terminated with a colon. Subsequently, a block of statements specific to the function follows. Functions have the capability to return a value to the caller, facilitated by the "return" keyword.
The function is now completely defined, yet executing the program at this juncture will yield no outcome, as the function has not been invoked. Therefore, outside the function's definition block, the function is called using sayHello().
examplePython Function Parameters
A parameter is a variable integrated into a method's signature, designated within the parentheses of the function's definition and separated by commas. When calling the function, the values are provided in a similar manner.
exampleDefault Argument Values
Python allows the assignment of default values to function parameters, a feature useful when users opt not to provide values. This is achieved through default argument values, assigned using the (=) operator.
exampleThe primary benefit of default arguments lies in the ability to assign values exclusively to chosen parameters while relying on default argument values for the rest. This facilitates more flexible and concise function calls.
exampleVariable number of arguments
Occasionally, programs may require defining a function capable of accommodating an arbitrary number of parameters, denoted as a variable number of arguments, achieved through the use of asterisks (*). This mechanism proves invaluable when the exact count of arguments to be passed to a function remains uncertain.
exampleReturning a Value
Beyond the capability to pass parameter values into a function, functions can also yield values. The "return" statement facilitates this process, allowing a function to conclude and return a value. Following the "return" keyword is an expression that undergoes evaluation before being returned by the function.
exampleInner Functions
The primary advantage of inner functions lies in their encapsulation, which shields them from external influences, effectively concealing them from the global scope and any external interference.
exampleIf you try calling the findSum(10.20) function:
outputAssign functions to variables
When you assign a function to a variable you don't use the () but simply the name of the function.
exampleFunctions as parameters
There are situations we have to pass Functions as parameters to other functions.
exampleConclusion
Python functions serve as self-contained code modules that perform specific tasks, enhancing code organization and reusability. They can accept parameters, return values, and even handle a variable number of arguments. With the ability to encapsulate logic and promote code modularity, functions play a key role in structuring effective and maintainable programs.