- Welcome to Lesson Four, Functions.
Up to this point we've used a few built-in functions
from the Python Standard Library,
and here we're going to start talking
about all the different syntax issues
for Creating your own custom functions in Python,
which of course is a key aspect
of Procedural Programming,
and then separately when we eventually do get
to Object Oriented Programming in Part Three,
we'll create functions in the context of classes,
which we would then called Methods,
and the same syntax issues apply there as well.
Now another key aspect of this lesson
is that we're going to start
to Import capabilities from other aspects
of the Python Standard Library.
So we'll learn the Import Statement
for the first time,
and that's going to be similar
to what you do in other C-based languages.
So for example, in C and C++,
we use the Pound Include Directive,
in Java we use the Import Keyword,
and in C sharp we use Using
in the same context.
We'll also demonstrate the concepts
of Passing data between functions in Python,
and because everything is an object in Python,
all you ever pass into a function
is a copy of a reference.
So in effect,
every object in Python is passed
into a function by reference.
And we'll talk about that and demonstrate
that to you as well.
We'll also step into some Simulation techniques
as part of this lesson,
introducing Random number generation
for the first time.
We'll talk as part of that discussion
about Tuples for the first time.
Tuples are another key data structure in Python
for aggregating data,
and one of the key uses of a tuple
is to be able to return
more than one value at a time
from a function.
So by definition,
functions in Python return one value at a time,
but that one value could be an object
and that object could contain multiple things,
and that's what tuples are commonly used for.
Amongst other things in Python.
We'll talk about how to create tuples,
which is called Packing a tuple.
We'll also talk about how to extract the data
in a tuple,
which is called Unpacking the tuple.
And we'll take a look at Scope
in the context of Python functions,
which is actually somewhat different
from how Scope works
in some of the other programming languages
that you're probably familiar with.
Some of the other topics
we'll look at in this lesson,
we'll show you how to do Default parameter values,
which are frequently used in Python.
We'll also take a look at what are known
as Keyword arguments,
which enable you to call a Python function or method
with the parameters named,
and that also means that you can change the order
of the parameters
and the function will still get called correctly
if you have named each of the arguments.
We'll show you how to define
a Variable-length argument list,
and we'll finish up
with a discussion of Methods
by demonstrating how to call functions
on objects of the string class.
So as I've mentioned previously
in the introduction,
it's very commonly the case in Python
that you will create objects of existing types
and simply interact with those objects,
and the primary way
that you'll do that is by calling their methods.