Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only 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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now