Python 3D Academyবাংলা

Functions in Python

Chapter 14: Functions

6 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. Defining and calling functions

    def name(): stores a block of code under a name without running it, and name() runs that block each time you call it.

  2. Parameters and arguments

    The names in the def brackets are parameters, the values in a call's brackets are arguments, and each call fills each parameter with its argument, in order.

  3. return: sending a value back

    return value ends the function and sends the value back to the place where it was called, so the caller can store it or use it.

  4. Default and keyword arguments

    A parameter with a default value can be left out of a call, and a keyword argument like greeting="Hi" names the parameter it's for, so order doesn't matter.

  5. Docstrings and helper functions

    Split a big job into small functions that each do one thing, and describe each with a docstring, a string on the first line of its body.

  6. Clean code: names, layout and PEP 8

    Clean code is code a reader understands at first glance: names that say what things are, small functions that each do one job, and the layout every Python programmer expects (PEP 8).