Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 24, 2022 04:51 pm GMT

Functions in Python

Functions are a set of statements bundled together to perform a specific task. A function can accept arguments that are manipulated to cause about a specific output or effect as intended.
Functions are often used in order to organize code, including getting rid of repetitive code, thus helps greatly in achieving the DRY principle in software engineering. DRY stands for 'Don't Repeat Yourself'.

Functions can be seen as executable code blocks and can be called once or more.

In Python we define functions using the def keyword followed by the function's name and finally the parenthesis. Parameters to the function are defined within the parenthesis.

When calling a function that accepts some arguments, we pass them in the parenthesis.

An example of a function:

def print_hello_world():    print("Hello World!")

When calling the function:

print_hello_world()

Output:

Hello World!

Functions can have parameters, parameters are used as placeholders or variables for inputs that will be passed as inputs during the calling of a function. Arguments on the other hand are values that are passed in a function as inputs to the function.

Functions with parameters

Declaring parameters when defining a function:

def sum_two_numbers(a, b):  sum = a + b  return sum

When calling the function we pass in the arguments, which in this case are a and b. For a = 3 and b = 4;

sum_two_numbers(3, 4)

Output:

7

When you take a look at the sum_two_numbers function, there is the keyword return. The return keyword is used to output the results back to the caller. It is also used as the exit point of a function.

Lambda functions

Lambda functions are small and anonymous(unlike the normal functions, they're defined without a name) functions, they take any number of arguments and return a result, however the can only have one expression.

A simple example of a lambda function that squares a value

x = 4# print a the square of xsquare_fun = (lambda x:x **2)(x)print(square_fun)

Output:

16

Docstrings in functions

A docstring is a string literal that is used in a function, class or when defining a class method. They give a brief description about the class or method, or a function in this case. It is not necessary to have a docstring but it is recommended.

The docstring of a function can be accessed using the __doc__ attribute.

Example of a docstring in functions:

def mul_number(a, b):    """ multiply  to values, a and b"""    return a*b

Getting the docstring of a function:

print(mul_number.__doc__)

Output:

multiply  to values, a and b

It is important to note that with functions or blocks of code in python, indentation is very crucial, it is easy to break code or have semantic errors, with unintended indentation.

This is a simple tutorial to give a brief introduction to functions from which you can lay a basis of understanding functions in python.

There is more to functions, being an explorer is one way to find out!


Original Link: https://dev.to/wekesa360/functions-in-python-34a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To