Composing a Function in Python

Free Video Tutorial and Guide

In this video, we're going to look at how to compose your own function in Python

Video Transcription

Hi, my name is Art and I teach Python at Noble Desktop. Today I'm going to show you how to compose your own function.

A function is a block of reusable code, something we can use over and over again. Let's create our first function.

Start with the keyword 'def', which stands for 'define', and then come up with a name for the function. The name should reflect the purpose of the function, so I'm going to call mine 'add'. This function takes two arguments (a and b). I want to define a as 6 and b as 7. Then I want to do a total of a plus b.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Now we usually print stuff, as humans we want to see what's going on. But in this case, we will use the 'return' operator, because a function always returns something. So I'm going to 'return' total.

Now I have this function, but nothing is going on because I need to call it to invoke the function. So I'm going to use the function name 'add'. You see I'm calling this function and I'm getting the result.

Keep in mind that 'return' should be the last statement within the function, nothing can be done after 'return'. Also, if I want to save the result of the function, I need to assign it to a variable, like 'variable_one' and 'variable_two'.

Now, these variables will hold the result from the function.

In my next video, I'll show you how to call a function within a function. See you in my next video!

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram