Python Function
What is Function?
The Function is a reusable block of code that plays a particular undertaking or set of duties.
Functions help in organizing code, making it modular, and selling code reusability.
Function Definition:
Function is a block of reusable code designed to carry out a selected undertaking.
Functions help in modularizing code, making it greater prepared, readable, and maintainable.
x = 10
if x > 10:
print("x is greater than 10")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
- The def key-word is used to define a function.
- greet is the name of the function.
- (name) is the parameter that the function takes. Functions can have multiple parameters.
- The colon : indicates the beginning of the function's code block.
Function Call:
Function call is the manner of executing a characteristic that has been described earlier in the code.
greet("Alice")
# Output: Hello, Alice!
This is the manner you call (invoke) a function. You offer the critical arguments in the parentheses.
Return Statement:
The return assertion is used to exit a characteristic and return a rate to the caller.
When a characteristic is called and encounters a pass again announcement, it immediately stops executing, and the desired fee is sent once more to the factor in the code wherein the characteristic have become known as.
def add_numbers(a, b):
"""This function adds two numbers and returns the result."""
result = a + b
return result
The return statement is used to return a value from the function.
sum_result = add_numbers(3, 5)
print(sum_result)
# Output: 8
Default Parameters:
Default values for parameters in a function. Default parameters will let you outline a price that a parameter will take if no argument is provided for it inside the route of the characteristic call. This is useful while you want a function to work with a default rate if the patron could no longer provide a particular one.
def greet_with_message(name, message="Hello"):
"""This function greets a person with an optional custom message."""
print(message + ", " + name + "!")
In this case, message is a parameter with a default fee. If no cost is furnished for message, it defaults to "Hello".
greet_with_message("Bob")
# Output: Hello, Bob!
greet_with_message("Bob", "Good morning")
# Output: Good morning, Bob!
Variable Scope:
Variable scope in Python refers to the region or context in which a variable is accessible and can be modified.
The scope determines where in the code a variable can be used and whether it can be accessed or modified from different parts of the program. Python has two main types of variable scope:
global_variable = 10
def print_global_variable():
"""This function prints the global variable."""
print(global_variable)
print_global_variable()
# Output: 10
Variables defined outside of any function have a global scope and can be accessed within functions.
Docstring:
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.
It is used to provide documentation and information about the purpose, behavior, and usage of the code it describes. Docstrings are not just comments; they are accessible through the __doc__ attribute at runtime, making them a form of documentation that can be programmatically retrieved.
def square(number):
"""This function squares the input number."""
return number ** 2
A docstring is a string that provides documentation for the function.
It is used to describe the purpose of the function, parameters, and return values.
Lambda Functions (Anonymous Functions):
A lambda feature, moreover called an nameless characteristic or a lambda expression, is a concise way to create small, unnamed talents. Lambda features are defined the use of the lambda keyword, located with the aid of a list of parameters, a colon, and an expression. The quit end result of the expression is the move again price of the lambda characteristic.
square = lambda x: x ** 2
Lambda skills are short, anonymous functions defined the usage of the lambda key-word.
result = square(4)
print(result)
# Output: 16
These are only some examples of the various techniques you can outline and use features in Python. Functions are a powerful tool in programming, allowing you to shape code, make it greater readable, and reuse desirable judgment.