Functions in Python with example


23 Views | Read Time 2 minutes | 0 Comments

Introduction

  • The function is a block of code which only runs when it’s called. It performs a single related action.
  • Function block begins with def followed by the function name and parentheses ().
  • Any inputs or arguments are put inside those parentheses
  • The code block within every function starts with a colon (:) and is indented
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return none.

Syntax

def functionName (parameters):

            “function_docstring”

            Function
suite

            return [expression]

Calling a Function

By defining a function and give it a name and a parameter,
we can call it anytime in the code

it gives an output the name that you have input into the function as a parameter, in that Case, it will give output: Ahmed

Parameters

You can add more than one parameter, as many parameters as you want, you should comma between each parameter and the one after

Also, you can add a default parameter, in case you didn’t add a one by yourself the code prints the default

It gives output

You can also pass a list as a parameter

It gives an output

Return Values

You can add the output values by using changing it from the
return

It gives an output

Recursion Function

Recursion is a common mathematical programming concept,  It means that a function calls itself. This
has the benefit of meaning that you can loop through data to reach a result.

Recursion function means that you can make a function that
calls itself several times

READ ALSO  Sphero Makes a Big Acquisition in littleBits to Bring Hands-On STEAM Learning to Life

And it gives output



Source link

?
WP Twitter Auto Publish Powered By : XYZScripts.com