In programming, a function is a reusable block of code that executes a certain functionality when it is called. Functions are integral parts of
every programming language because they help make your code more modular and reusable. In this article, I will show you how to define a function in Python and call it, so you can break down the code of your Python applications into smaller chunks. I will also show you how arguments and the return keyword works in Python functions. In Python, you define a function with the
def keyword, then write the function identifier (name) followed by parentheses and a colon. The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you. Following the basic syntax above, an example of a basic Python function printing “Hello World” to the terminal looks like this: To
call this function, write the name of the function followed by parentheses: Next, run your code in the terminal by typing python filename.py to show what you want the function to do:Basic Syntax for Defining a Function in Python
Basic Examples of a Function in Python
Another basic example of subtractig 2 numbers looks like this:
def subtractNum(): print(34 - 4) subtractNum() # Output: 30Arguments in Python Functions
While defining a function in Python, you can pass argument(s) into the function by putting them inside the parenthesis.
The basic syntax for doing this looks as shown below:
def functionName(arg1, arg2): # What to do with functionWhen the function is called, then you need to specify a value for the arguments:
functionName(valueForArg1, valueForArg2)Here's an example of arguments in a Python function:
def addNum(num1, num2): print(num1 + num2) addNum(2, 4) # Output: 6In the example above:
- I passed 2 arguments into the function named addNum
- I told it to print the sum of the 2 arguments to the terminal
- I then called it with the values for the 2 arguments specified
N.B.: You can specify as many arguments as you want.
How to Use the Return Keyword in Python
In Python, you can use the return keyword to exit a function so it goes back to where it was called. That is, send something out of the function.
The return statement can contain an expression to execute once the function is called.
The example below demonstrates how the return keyword works in Python:
What’s the code above doing?
- I defined a function named multiplyNum and passed it num1 as an argument
- Inside the function, I used the return keyword to specify that I want num1 to be multiplied by 8
- After that, I called the function, passed 8 into it as the value for the num1 argument, and assigned the function call to a variable I named result
- With the result variable, I was able to print what I intended to do with the function to the terminal
Conclusion
In this article, you learned how to define and call functions in Python. You also learned how to pass arguments into a function and use the return keyword, so you can be more creative with the functions you write.
If you find this article helpful, don’t hesitate to share it with your friends and family.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
What Is Syntax in Computer Programming?
JavaScript 101 is a series of instructional videos given by Devmountain Web Development Instructors. Click here to see the entire video series.
We refer to syntax in computer programming as the concept of giving specific word sets in specific orders to computers so that they do what we want them to do. Every programming language uses different word sets in different orders, which means that each programming language uses its own syntax. But, no matter the programming language, computers are really exacting in how we structure our syntax. In this video, you’ll understand what syntax is in programming and how it’s important in getting a computer to do what you want it to do.
Video Transcription
Once we figure out what we want the computer to do, we need a way to communicate to give it the instructions so that it can do it correctly. This is where we’re going to write the code part of coding.
What is Syntax in Programming?
Syntax is the set of rules that define what the various combinations of symbols mean. This tells the computer how to read the code. Syntax refers to a concept in writing code dealing with a very specific set of words and a very specific order to those words when we give the computer instructions. This order and this strict structure is what enables us to communicate effectively with a computer. Syntax is to code, like grammar is to English or any other language. A big difference though is that computers are really exacting in how we structure that grammar or our syntax.
This syntax is why we call programming coding. Even amongst all the different languages that are out there. Each programming language uses different words in a different structure in how we give it information to get the computer to follow our instructions.
Web developers primarily focus on HTML, CSS, and JavaScript. That is what we’re going to focus on in this course as well. By focusing on these languages and mastering them, you’ll be able to write websites that can be opened by any browser in the world.
Programming Syntax Examples
To explain syntax further, we’re going to define syntax as essentially a set of keywords and characters that a computer can read, interpret, and convert into tasks that it knows it needs to do. Programming or coding is typing these keywords and these characters into a file in the right order and feeding it into the browser. The browser then processes our information, if it’s in the right order, it then can figure out how to execute the task that we want it to do.
Think of syntax like the drive-through at a burger joint. If you can do a few simple things in the exact right order, you get what you want. You have to tell them what you want in the microphone, pay at the first window, and pick up your food at the second window. You have to do it in that order. You can’t pay the microphone or pick up your food where you’re supposed to pay. Additionally having this order in place helps you get what you want faster and with less talking.
Because we’ve already agreed how drive-throughs work, you can simply pull up and say, give me number one and expect to get exactly what you want. That’s the benefit of having preset syntax.
Computers Can’t Read Typos
The flip side of syntax is that computers are going to be exact in what they expect us to code. Humans are not so precise. We can read someone else’s typo and infer what they meant and move right along. If a computer finds a typo, it’s gonna get confused and lost, and have no idea what you meant it to do. This might get you pretty anxious about all the mistakes you can make on a computer. It’s nothing to get worked up about. If you make a mistake, your computer is not going to crash and catch on fire. It’s simply gonna say, “This doesn’t work.” You fix the error, you try again.
If you forget something, there’s a lot of online guides and references that you can look up to find the syntax that you’re missing. So don’t get worked up about it.
Let’s move on to some more exercises so that you can practice this and start getting more familiar with syntax.
Continue Learning
How Long Does It Take to Learn Coding?
Software Developer vs. Software Engineer: What’s the Difference?
What Is Data Science?: A Beginner’s Guide