An important technique when we write code is to not repeat ourselves. That is, avoid writing the same code over and over and over again, we call this DRT. DRY means don't repeat yourself. If you're in one of my other classes, you may have heard me use this term already. So today we're gonna learn to write DRY code using Function s.
First, let's review the previous lesson. In the last lesson, we introduced loops ways to repeatedly execute a block of code until a condition is satisfied. While Loops are used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed. A For Loop is used for iterating over a sequence. This can be a list, a tupple, a dictionary, set or string. A Nested Loop is a loop inside a loop. And we learned about how the Break Statement in Python terminates the current loop and resumes execution at the next statement.
Great, let's jump into FUNCTIONS. But first, go to Replit.com and create a new Repl and name it PGD_7_Functions.
A Function is a set of code that is grouped together in terms of functionality has a name, and using this name, uses its functionality wherever you need it. We've already used a lot of Functions provided by Python, do you remember anything? Whenever we want to print a string to the console, we use the Print Function. This is an example of a prebuilt Function in Python that saves us from writing commonly used functionality. Using Functions makes your program easier to understand and develop. They also help us share pieces of functionality with other coders.
There's a particular way of defining Functions and we need to follow that structure. Let's start by learning that a Function has two parts. First part is the Header that tells us the name of the Function . The second part is the Body. The body contains the code that implements the actual Function ality, we tell Python that a new Function is defined using the def keyword, we name the Function using def, and then use a colon to separate the header from the body of the Function. Here's the syntax. To use the Function , the name, referred to as calling the Function must be written followed by parentheses.
Functions have a capability that makes them very, very powerful. A Function can accept data inputs from Python, and use these inputs to perform the operation. The inputs are called Parameters, and Arguments. Here's the syntax.
Let's look at an example. Imagine you want a Function called “addition” that prints the sum of two numbers. To do this, we'll define a Function called “add”. And then we'll define two Arguments “num_1” and “num_2” inside the parentheses next to the Function name. When we call the “add” Function with values 2 and 4, then X is assigned the value 2, and Y is assigned the value 4, and then the sum of 6 is printed. Two and four are called Arguments. If we call the “add” Function with six and 12, the Arguments will be six and 12. And the Function will print 18 in the console.
If the difference between Parameters and an Arguments sounds confusing, remember, Parameters are the placeholders for the Arguments and are defined when you create a Function. And what are Arguments? Arguments are the values provided to a Function when you call it.
Now we're going to talk about the next concept of this unit Return Statements. Inside your Functions, you can use a Return Statement to return a value from your Function . These are very useful in case you want to pass any value out of the Function and then use it in the rest of your code. Here's the syntax. Here's a simple example that adds two numbers X and Y and then return the sum.
Calling a Function inside the body of the Function itself is called Recursion because this causes the code to run over and over and over again basically forever. Well, this isn't good in most cases, to avoid repeating the same code forever use a return keyword to break out of the recursion. Here's an example. The Function factorial is calling itself if X is not equal to 1, what would be the output when this Functions runs?
Alright, great work today. I'll see you again soon.