C-SHARP - 4.10 Procedural programming

Hello everyone and welcome to this new video. In this video, we will address a very important notion that procedural programming. But what is procedural programming Procedural programming is a programming paradigm based on procedural calls, say like that it may be a little blurred. In truth, procedural programming is divided our program into small parts called procedures or functions or method is the same. As its name suggests, procedural programming contains a step-by-step procedure to be executed. Here, the problems are broken down into small parts and then, to solve each part, one or more functions are used. Until now, all the code we wrote in our exercises was in the main method, the hand method. However, this is not how we build applications of any type, because an application in general includes a large number of features and writing all our code in the main method is a disaster. We must therefore break down our code into several methods or functions, each being responsible for one thing. And this is what we call procedural programming. We also have object-oriented programming that is another object-based programming paradigm. Thus, instead of thinking about methods and functions, we think of objects. This will be a subject that we will discuss in the 2nd part of our course. But for the moment, let's focus on this notion of procedural programming because we must first understand and master this notion before moving on to another. Another thing, we were talking about procedure, method, functions, I know it's a lot of words, just remember that all these words mean exactly the same thing. In reality, it refers to a subprogram to perform repetitive operations. Instead of writing the full code in our hand method, we will divide our program and call these functions only when we need them. But also, they allow you to perform repetitive tasks, no need to rewrite the same program every time, for example, to calculate the average of each class. Here we will create a reusable function and adapt automatically. We will just have to call this function and give it the values it must manage. So now, I will show you how to apply this notion of procedural programming in C# with a simple and clear example. Let's move on to Visual Studio. Until now, if we wanted to design a program that will calculate a student's average, we will proceed like this. We will create an array that contains several notes. We type var, we give it the name notes then = new int brackets braces and we will put several notes. 10 comma 12 virugle 20 comma 6 Here little reminder, if we put our values directly in the braces, we do not need to specify the number of items I want to store in my table it's done automatically. On the other hand, if we want to assign them later then we must specify the size of our table. Now that we have our table, we know that the average is the sum of all the scores divided by the total number of scores. Either here, we will have to browse our table, added each note and then / by the number of notes in our table. So to browse our notes, I have to make a loop. I make a for int i = 0 virugle point as long as i is less than the number of notes in my table so you loop either notes.Length and i++; Then in the braces, I have to add my notes each turn so I will create an average variable right here that I will initialize to 0. And at each loop turn, we will add what average contains with the note at the index of i. We average = average + scores of i. Either in the first round average is = at 0, i is = at zero so we will retrieve the note at index 0 or 10 and we will add it with average. 0 + 10 = 10. So average now contains 10. Then in the 2nd round, i is equivalent to 1 since it has been incremented, the score in index 1, i.e. 12, is recovered and it is added with average. Now we have 22 and so on until the end of the table. And in the end, average is equivalent to the sum of all the notes in the table, or how much is it if you are strong in mental calculation? Haha exactly 48 that's right. Now to have our average, we must divide by the number of notes in our table. So here I make average = average divided by notes.Length to have the number of notes in the table. This gives 48 divided by 4 And I make a Console.WriteLine() to display the average in quotation mark parentheses The average is of and I concatenate with the average variable. I'm launching the program and you see? We have our average that has been displayed "The average is 12" Very well, here the code is still readable, short etc... But the problem, let's imagine now our program must calculate the average of 28 students in a class, so we will copy and paste this program 28 times? Impossible, our program will first be very long, slow and we will waste a lot of time writing everything. And if our program is to be used in an entire college either the number of students can very easily rise to 1000 2000. Are you going to write this code 2000 times? I don't think so. Well, that's the usefulness of procedural programming. We will take this program and adapt it so that it can manage no matter how many students there are in a college. So how to do this in C#. Here, as we can see in our program, there are 2 things. We have a student's grades and here all the logic to calculate the average and display it. Well, what our function will contain is really all the logic because it does not change, the way we calculate the average will never change, it is simply the values that will change. So we don't need to initialize an array in our function. It's like the functions we saw in previous videos. For example ToUpper ToLower, we applied these methods to a string we had created but the logic that allows you to transform characters into uppercase or lowercase is contained in the ToLower or ToUpper method/function. I hope it's a little clearer for you. To create a method/function in C#, you see here we have the main Main method which is our entry point. This is a somewhat special method because it is called directly when we launch our program. When you click on play, you come directly on this method. We will create a new method, and this method we will have to call it from the main program. This method will give it an explicit name that will allow us to know what it does, or here we will call it CalculateMedium and this method will return a result. The result that it will return to us is the average of a student or an int so the return value of the method is an int. And this method takes an array of notes as input. When we add something in the parentheses of a method/function, it is a way to send information to our method so that the method can use it. And in this method, we must send our table of notes in the method in order to calculate the average. Either the method will take a parameter an array of int so int note brackets. Ok, so from there I now have the information and I could reuse it in our logic, we can put any table. The program will take care of it. And there I will add braces to be able to add the code of my function. All right, just for now, we will add the static keyword. For the moment, voluntarily I do not explain to you but it will be a subject that we will discuss especially when we talk in more detail about object-oriented programming. So CalculateAverage what are we going to do? Well, I'm going to resume the program we did just before, I do an x check and I stick it between the braces. Ok, what do we do inside this function? Well, we have an average variable that is local to the function. That is, it will only be accessible in this function. And then during our program, she will calculate the average and store it. And at the end of our code here, we will remove our Console.WriteLine and we will return our average. So far, what we have done is called the definition of our method. Now, we are going to do what is called the function call. We will come and call this function from our main program. And we will store the average that the function will return in an average variable. The average variable is no longer declared in the hand because the code has been moved and the declaration of this variable is here. I have the right to recreate an average int variable that has the same name as the variable used in the CalculateAverage() method because here it is just declared as part of the function. This means that the method has its own average variable and the hand method, also its own. Then we could have called him differently. But that's why we didn't have an error, no conflict because each function has its own local variables. And then in the parentheses, I have to give the table on which I want to calculate the average. I will do several so that you can see how it will go if we have to average several students. I'm going to do var eleveUn = new int crochet braces and I give him notes…