Skip main navigation

£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

Find out more

Introduction to Functions

This video provides an introduction to functions.
<v ->So functions name a set of statements in Python</v> and then when you invoke that function, those statements will be executed. So let’s take a look at this example. So we have two function definitions, so everything that’s part of this, everything that’s highlighted as part of the function definition, so there’s one for calc underscore per there’s one for main. The header is just this first line that ends in the colon. That’s the header. The name is the whatsever following the def, the keyword def says this is a function definition, and before the parameter list. This is a list of names for values that we passed into the function.
And notice you can specify a default value here. You can also, when you call a function, these are both function calls. So we use the name of the function calc per followed by a parameter list, in this case, we call it with just three. So B will have a default value of four. Whereas here we’re naming each of the parameters or each of the arguments that we’re passing in. So we’re saying, hey, set A equal to eight and B equal to ten. So it’s one of the things you can do in Python. This is the actual also actually a function call, right, calling this main method.
Notice that functions don’t have to take any parameters and you can return a value from a function using the return keyword. You don’t have to return anything. The main method doesn’t return anything. And so that’s a quick introduction to functions.
This article is from the free online

Big Ideas in Programming: Expressing Yourself with Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education