Podcast Lookup

Java Methods Explained | Recursion, Method Overloading & Functions Deep Dive

The Java Developers Hub

  • December 3, 2026
  • 1 h 25 min
  • Episode 11
Episode page
About this episodeMethods are essential building blocks of Java programming. They allow developers to organise code into reusable units, reduce repetition…
Methods are essential building blocks of Java programming. They allow developers to organise code into reusable units, reduce repetition, and create applications that are easier to understand, maintain, and extend.In this episode, we take a deeper look at Java methods, exploring how they work, how to pass arguments, and how to return values. You'll also learn two important programming concepts: recursion and method overloading.Through practical examples, you'll understand how methods can call themselves to solve complex problems and how multiple methods can share the same name while accepting different parameters.Whether you're a beginner strengthening your Java fundamentals or an aspiring software engineer preparing for advanced programming, this tutorial will improve your understanding of Java methods and problem-solving techniques.What You'll Learn
  • Understanding methods and functions in Java.

  • How to declare and define Java methods.

  • Calling methods from the main() method.

  • Passing arguments and parameters to methods.

  • Understanding return types and the return statement.

  • Differences between methods that return values and void methods.

  • Understanding method overloading.

  • Creating multiple methods with the same name but different parameter lists.

  • Understanding recursion in Java.

  • How recursive methods call themselves.

  • Understanding base cases and recursive cases.

  • Practical examples of recursion.

  • Common recursion mistakes and stack overflow errors.

  • Best practices for writing reusable Java methods.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Java Methods Explained | Recursion, Method Overloading & Functions Deep Dive

00:00—
  1. Speaker 1

    Yes, so let's start. Welcome back Coder Army to our Java playlist. And today we are going to start a new topic, Functions. Now if you come from a programming background, then you know what functions are. But if you are fresh and doing Java for the first time, then you will have a lot of fun in understanding functions. And now we will go a little bit towards advanced concepts from basic Java syntax. Once you understand your functions well, then your base will be set to understand object-oriented programming. And this is a very important thing. So let's quickly go to the screen and see. So what are the functions in Java? So before that, let me tell you a scenario. So let's say you are writing a Java code and again I have made a code block here. Here you know you have written something like public static void means and you write code in brackets. So I am telling you that you have to write a code that you have to take two integer i and j and print their sum. So you will say that no problem, we will do one thing, we will take an int i, let's say equal to 4 and we will take an int j, let's say equal to 5. And what did you tell me to print the sum? So I will do one more thing, I will take another variable int sum and I will do it i plus j. And if I want to print, then I will println So my work is done. But now let's make it a little lower. Let's say your code is written here. Now suppose after writing this line you do some other work in your code And then you remember that you have to add two numbers again So you will have to take two numbers again Let's say this time int ij was done So k and int let's say l you took two variables This time you took 9 and 10 Something like this Then you will make a variable and add k plus l And then you will print it. Right? Then you start writing some code. And then you know that you have to add two numbers again. So, do you understand the problem? What is happening? We have a repeated code. Why? Because we have to add two numbers in our code again and again. Right? So, here we understand that we are not able to reuse the code. Basically, we are writing one code again and again. Let's say this code. We declare two numbers, remove its sum and print them. This work, let's say we have to do it again and again in our code. So, we have to write its code again and again. Right? From here, a little concept of your functions comes out. Okay? So, first we define the function. What are functions? So, we are talking about functions. So, consider a function as a block of code. So, again I will make a code block like this. So, function is a block of code. Let's say this. And what do we write in this block of code? We write anything in this block of code which we have to do repeatedly. So, let's say I have written a function to add two numbers. Now the normal code that was running here, in which whenever I have to add two numbers, I will call this function that you add two numbers to me. Now calling means in terms of programming, I will call this function that you add two numbers to me, then I will do the rest of my work. Then whenever I have to add two numbers again, If I want to add two numbers, I will repeat this function. If I want to add two numbers, I will repeat this function. If I want to add two numbers, I will repeat this function. If I want to add two numbers, I will repeat this function. If I want to add two numbers, I will repeat this function. Then we can define the function a little. I told you that we have to add two numbers. So let's go back to the same thing. If we try to write a function of it. You will also get to know how we write functions in Java. So first of all, I will show you a boilerplate code which we always get in Java. So see, what I did here, here in my public static void, whatever boilerplate code we have already written, I wrote that. So that I can explain to you that whatever code we are writing, we are writing inside this boilerplate code. So what I told you, I have to add two numbers. So let's say I took two numbers here, I took an int i, again 4. And I took an int j, which is let's say 5. Now I have to add two numbers and I know that I have to add them in this code again and again So I make a separate function of it Now whenever I make a function in Java So this boilerplate code that I had written Public static void main I make that function outside of it So let's say when we look at the code, we will make it below it, now I am making it here So let's say I give the name of that function, first of all The name of the function will be sum Okay Let's make it a little further, let's say here, sum I have given the name of the function here. I am explaining the entire syntax to you. Now what will this sum do? It will get the sum of two numbers. So first of all, I will give my brackets and I will have to take two numbers whose sum it will get. So let's say int a comma int b. Here I took two numbers in the input and I turned on the curly bracket here and turned off the curly bracket here. So first of all, we have the name of the method, we gave it the name sum Then we gave it two parameters, I will also tell you one by one What are these? Basically, the two inputs on which it will work Now what will it do? I know it will do something like this Let's say it will make an input by doing an int result, let's do it a little bit here It will make an int result, it will add a plus b to it Now as we have written this line, I have to write another line which I will call return. This keyword is return and then I will write result. So see what keywords I have written here. The first keyword I have written here is return. Here obviously the name of the function is not a keyword but the name of the function is written. Rest is the same. Now you understand what I have done. First I have taken the name of the function. Then I wrote a result variable and added a plus b sum to it. Then I wrote the keyword return and then we wrote the result. So you must have understood what we want to say. First of all, let's take out a result. And whatever value we get, whatever work this function has done, we have to give its output. So we write the keyword return. Return means this function is returning us. So what is it returning us? It is returning the result. And what is the result? It is the sum of a and b. a plus b. Very good. Now there are some more things that we have to write. First of all, the name of our function, sum, before this, there is a place here. Here we have to write what is the return type of this function. Now return type is nothing. So I will tell it here. Return type. Return type is nothing. Whatever is your function, it returns the data type. That is what we call return type. So return type is nothing. Data type that is returned by function. Simple. That is returned by function. So that becomes your return type. Now in this case, our return is result. And what is the type of result? Integer. So the return type of this function is integer. So we will write int sum. Int a, b. So first of all, you must have understood that we have told the integer, i.e. the return type. That what function will give us. Then we wrote the name of the function. Then the input on which it will work. Then we did some work like we calculated the result. And finally we returned that result. Very good. Has our function been completed yet? Yes, it has been completed. There is just one more keyword that we have to put here, which we call static for now. And why do we have to put this keyword? We will not discuss it now, we will leave it for later. Again, consider it as a black box that you will have to put this keyword. After oops, you will understand why. But this syntax is complete. Now if you want to declare any function, you will write static like this. The return type of that is integer because we are returning int result. The name of the function on which it will work is inputs. And finally a return statement. So here we have written a function to add two numbers. Now see what I can do in my code. I will make it a little smaller. Now whenever I am writing the code int i, j Now I have to add these two i, j So what will I do? I will not write the function to add by myself I will work that I am going to get something, let's say result And how will I get that? I will get that by calling this function What does calling mean? That I will write the name of this function, sum And I will pass the inputs that I had to pass in brackets So I have to pass i, j Now I have to call this function sum i, j which is this. Now this function will run and will give me a result. And what will that result give? It will take these i and j in the input. Here there is a, b, it's okay. This i will be set from a and this j will be set from b. will be set on b, basically nothing happened, I told here what I am actually giving i, j and here sum said that what I take, I take int a, int b, two variables, now this i variable will go to a, j variable will go to b, then I will calculate result a plus b, means 4 plus 5, 4 plus 5 will be 9, result will be 9, 9 will come back here, 9, Now we have assigned the assignment operator to the result variable Now as soon as we have assigned it, then I can do my system.out.println result Something like this And what will be printed on our screen? Our screen will be printed as 9 Okay? So I hope you understood this. Now in this code, whenever we call this sum, we can get it done and give different arguments. Let me tell you what the arguments are. We call the sum again below. Let's say this time I don't take a variable and give a straight answer. 10, 9. I just passed the literals, no problem. Now what will happen? This 10 will be assigned to A, 9 will be assigned to B. It will give the sum of 10,9 which is 19. And what will be my return? I will get 19. Now I can give this 19 optionally in any variable. Let's say the name of the variable is C. So I took a variable C. In that C, I can assign 19 as I did here. Okay, so this is the concept of all the functions. I hope you got some clarity. Now, let's look at this function a little carefully. Static int sum a, b and let's monitor all its things once. So, let me tell you. Look, first of all, if we start labeling it, then let's say we label it with red pen. Okay, so what did I say? This is your return type. Okay, we have given the return type of the function here. Very good. This is the name of the function. I am not labeling this, you can assume that you have to write this. We will discuss this later. Now the int a, int b in the bracket is called parameters in java. Means the function which is taking parameters. Now see how it has taken parameters. One data type's name, variable's name. Data type's name, variable's name. So it said give me two integers. A and B. Now if you give integer, then you know how we declare integer. Int A, Int B. So it said give me two integers, I can work on that. So we call this parameters. Okay, very good. And finally, this is our return statement. This is returning parameters. a data type or returning an output. You write it like this, returning an output. It is useful for this. Okay? Very good. So we have labeled all of these things. Now one last thing. Whenever we use this function like this, that sum i comma j, we call this calling a function. What do we call in Java's terms? Calling a function. That we called a function. Now, by calling, it must have given us some output. We assigned it to this result variable. Okay, one more thing. Now, when we call a function, how do we call it? The name of the function is sum. And here, I, J passed. Now, the variables that we pass here, we call them arguments. Okay? There are just some terms. Now, understand this way. Argument is the exact argument. There are literals or exact numbers that we are passing. Like in this case, I passed i, j, i.e. 4, 5. These 4, 5, which will be set here, will become parameters. Parameters means a function that I need two things, i and j. Or a, b. Just give any two integers. Name can be anything. Their name can be i, j also. It doesn't matter. Sum function says that give me anything in two inputs. I will calculate the result and give you. I will calculate the sum and give you. When we call it, we say, take these two inputs, i and j. So, when we are giving input, that take these inputs, these actual numbers we are giving it, these are arguments. And the input that it was asking, that it was expecting, it was expecting anything, that give me any two numbers. I don't know what you will give me, you can give me anything. So, here these were just variables. So, we called them parameters. So, You can imagine what will happen in memory when this sum function is saying that I want two arguments of integer type. So till now a and b have not got any value. So in container a and b are lying like this, let's say empty. They say give me any two values here in a and b. I will calculate their sum. But when I actually give i and j, actually I gave 4 and 5. Now it works on them. So the 4 and 5 that I gave are arguments. And what I was expecting here are my parameters. This is nothing, these are some naming conventions that we use in java that what is what. If you get the idea of all these, then we will move ahead. Now look, the functions that we have studied till now, you should understand it like this, that we have studied functions in mathematics also, right? We have studied PA that function is nothing in maths. Let's say f of x, you give it an input, it does some processing on this input, it can do anything, and finally it gives us an output. Mathematically, we define functions like this. In Java or any programming language, functions are not that different. You see, our function, our code block was to get the sum. So, let's say he did something like this, sum a, b. I am not writing the whole code. I am just assuming that we have written a function here. So, this was our function, f of x. What we used to do is, we used to give input on it. Let's say we gave input above i and j. We gave input here, this is i and j. So, let's assume that we used to give it an input, i and j. And this used to give us an output, result. So, this is our output, this is our input, these are our inputs, and this is our function. This means that we are understanding that our functions in Java or any programming language are almost the same as we define them mathematically. Now, let's move ahead and see what types of functions we have. So now we are talking about types of functions. So first of all I will tell you what types of functions are there. We have categorized them very simply. Until we are not reading oops, we will call functions functions. Once we will read object oriented programming, we will call functions methods. And then I will tell you why. Now the categorization we have done is something like this. That a function can be like this. Which neither takes any input, no input. And neither gives any output, no output. The code we saw till now was taking input as well. You might remember that i, j takes input and the result gives output. So it was a function that takes input and output as well. So you must have understood how we are categorizing. No input, no output. Another one might be that it will take input but no output. Another one might be that it will not take input But it will give output. And the fourth one is, It will take input as well as output. So, did you understand? We covered all the four possibilities. No input, no output. There is input but no output. There is output and there is no input. And both of them are there. Okay? So, the code that we just saw, You must have remembered. What kind of code is that? It used to take input as well as output. Okay? If we go back to that function, So, this was our function, sum. Okay? So, it took two inputs, A, B. Output gave result. And we wrote return type here. Okay? So we can make all types of functions which we have covered below, no input, no output, input, output, etc. We can make all types of functions. Now I will tell you this one by one, it won't be fun. We will see this quickly in code. How are all the categorizations? You will enjoy it more in that. So let's go to code quickly. So see here I have written my boilerplate code again. It is made in a class called khali-demo.java. So we are talking about functions in java. So let's see this quickly. Now, what did I tell you? Let's say, first of all, I make a function that calculates two numbers. Or let's do one thing. Let's make all the functions that we talked about. No input, no output, input, no output, etc. We make all the possibilities one by one. And I also told you that whatever function we make, we make it out of this block of code. We make it out of the block code of public static void main. So, let's say I came out of here. Now, I made a function first. Let's write here, let's say, no input. This type of function and no output. We will neither take any input nor output. Now you will understand the use of this function. Let's say I am making a function. Whenever you call it, it will say hello. So first of all, I said that static will come. You can accept it. Now see what will be its return type. So I am leaving it empty. Now I will tell you what the return type will be. Now I will give the name of the function. Let's say greet. Now see here I have left the bracket empty which means no one is taking the input Then I will start my block of code And let's say I will write simple System.out.println Hello So what I did is I printed hello on the screen and didn't write any return type here If I am not returning anything or not returning any data type then I can simply write return Return I have written simply return because I am not writing any particular data type return. I am not writing return result or return any variable. I am simply writing return. Basically this function is empty and does not return anything. If any function is empty and does not return anything then we put void there. This void is a special keyword that tells our Java compiler that the function that we have written is not returning anything. Void means that there is no availability in it. So we just put a keyword that we are not returning anything here. So we can write an empty return here. In fact, this is also optional. Optional means I can remove it. Remove means I can comment it. I can write it as void grid system.out.printl hello. So let's make it optional. You can apply it or not. Something like this. So this is our no input no output. Let's make another function which will take input but no output. No output. something like this. It will take input but not output. Now what does this mean? Let's say I make another function which will say hello but with your name. That means it will take your name input. So just to print on the screen, it will not give you any output. So again I will write static and it will not give you any output. So void because there is no output. And again I will say it say hello. Whatever name I put, I called it greet, I called it say hello. What will it do? It will take a string input, let's say name. Okay. So what I have said here is that these are its parameters. It takes a name in the input and simply prints it on the screen. System.out.println And now how does it print? First of all it says hello, then a little space and then we concatenate this string with your name. Simple. We had seen this. What do we do by applying the plus operator? We concatenate two strings. And we are not doing any return. So I can write a return here. So I don't write in it. So it took an input, a name, but didn't give you any output. Now you must be understanding one more thing that your number of parameters can be anything. Number of parameters means, let's write it completely here instead of no. number of parameters because here we took only one parameter name when we made the sum function we took two variables int a comma int b right so number of parameters can be any number of parameters here is only one parameter of string type there are two parameters of integer type no problem now we make a third type of function which doesn't take input but gives you output no input but output ok So how can we make this? Very simple. First we will write our static keyword. Now it will give you output. What type of output will it give? Let's say it will give an integer type of output. I have just written integer. Now let's name it. Let's say get number. Now I can't write anything in this bracket because it won't take any input. I will start my code block. Now I have to return an integer. So I simply write in a line return let's say 10. Or I can return any number instead of 10. I wrote getNumber is a function that always gives you 10. Or you can put a random function in it, which can bring anything from 1 to 10. Right, we will read that later. But now you see, I did nothing, I made a simple function, getNumber. It will return you 10. Now you will say, what was the need to make a function for this? There was no need. This is just to explain to you that there can be a function of no input and no output. That it will give an output of integer type. So we have written return 10. But it will not take anything in the input. Okay, so this is also possible. And finally, we have the one that takes input and also gives output. Like we made our sum function. So this time let's make it multiply. So again our static keyword. We will take integer type return. We will return with any function. And we will take integer type return type. And this time let's write multiply. Okay multiply. What will it do? It will take two numbers a and b. And it will return their multiplication. So we can do it like this. Let's say we took a variable mul and multiply. And this will multiply a with b and return multiply. In fact, I can write these two lines in one line and I can simply write return a into b in this way. Something like this. So you understand, I can write like this also. I simply say return. What to return? Multiply A and B and return. Now you know how Java works from inside. First it will multiply A and B. And it will give an integer value. Because A is also an integer. And B is also an integer. So they will get an integer by multiplying. Now it will return that integer. And who will it return? Whoever is calling this multiply. We will see that now. Okay. So we have made all the functions here. No input, no output. Input, no output. No input, output. And input, output. Now our job is to call these four functions in our code. Where we used to write code. Till now, we haven't come out of this code block. And for the first time, we have written functions in different code blocks. Now let's see how we call the function. So first of all, I do one thing. First of all, I call my no input, no output. So I write greet. Greet. So as soon as I called greet, what will happen to me? Hello should be printed on the screen. And I didn't do anything. See, my entire code is empty. Functions in Java are commented here. I just called one function, greet. Now if greet is called, then what will happen? Now understand a little flow of control. Our flow of control, you know, runs line by line. So first of all it will skip the comment and then it will come to the greet. Now it will see a function is calling. So as it will see a function is calling, it will directly jump towards that function. Now how it jumps, what is happening from inside, we will discuss that later. But now suppose it jumped towards this function. Now it has started executing here. First of all it has printed hello. So it will come to our screen, hello. Then we will return from here, this is also commented. No need for this, let's write return here. Okay, then he printed hello and returned it from here. Where will he return it? From where he called it. So it came back here. In the next line, space. In the next line, our entire code is finished. Our code will stop running. That means, only hello should come on the screen. So, let's see it quickly. This is our hello. Very good. Now, let's do one thing. We called greet. Now, without calling greet, let's comment this. And this time, we call say hello. Say hello. Now I have to give a string in this sayhello, right? So I can give any string here. So here I declare a string above, let's say name and I call it Aditya. And here I pass the name, something like this. Right? Or I can directly pass Aditya here, something like this. Something like this, okay? I don't need to declare it. Simply, what I did here, the thing I pass here, what do I call it? Argument. We have already discussed that we call it arguments. Now I have called sayhello here. By giving an argument whose name is name. Now see my vs code has written name.dot here. I haven't written it. It has written it by itself. Why? Because it knows that whenever I am calling a function named sayhello, it is expecting one parameter name. And here I have given the argument aditya. So it has defined itself that name is aditya. I just wrote aditya here and it came automatically So what will come on my screen as soon as I write hello aditya? As soon as our flow of control goes into this function, our print will be hello plus name, whatever name I give, that is, hello aditya So let's see if hello aditya is being printed on the screen, it is being printed Now whatever name I give here, if I give the name rohit, then hello rohit will be printed So our function has become a reusable code. Now if I want to say hello to someone, I will directly say hello and write their name. I don't need to write this line. Now this is a small line. But suppose I am doing a lot of work in this function. First I will add their two names and print their age. But suppose I am doing a lot of complicated work. Now let's do one thing, we can also call both functions at the same time. First we called greet, then we called say hello. And here first will be hello, then in the next line will be hello Rohit. Now let's see this too. Hello, then hello Rohit. Very good. Now, we will call our third function. Which is, let's say, getNumber. Now, let's do one thing. We have called getNumber. Now, what will getNumber do? It will give us a number. So, that number is put in some variable. Let's say, x. We took a variable and called getNumber in it. Now, what will getNumber do? It will give you an output. For the first time, we have taken an output. What will it give you? It will give you 10. We have assigned that 10 to a variable x. We want to So we can print this variable. Let's say I wrote x. So something like this. Our x will also be printed in the third line. See, first hello, then hello Rohit, and then 10 is printed. Now, the interesting thing is that I can write these two lines here and do it directly like this, that the method of getNumber that I have called, I can directly put it inside this and skip it. So, see what I did, system.out.println, I have to print something, what do I have to print? Whatever method returns, so I directly called getNumber here, my getNumber must have been called, it must have returned 10 to me, and where will this 10 come? My 10 will automatically come inside this bracket and my 10 will be printed. So this will also work. See this. So you can do it this way as well. So here I have passed the direct argument. First I declare the variable and then pass it. I passed it directly. Same here. I called the direct function. System.out.println. Get number. Okay. Now one more thing. If I don't want to print this. Let's say I directly call getNumber. And I'm not taking it in any variable. Earlier I was doing this, int x equal to getNumber. If I'm not taking it in any variable, then I'm not getting any error. I can basically call a method, which will give me an output, like 10. But if I want, then I shouldn't collect that output. I shouldn't collect it in any variable. There's no issue with that. But nothing will happen with that. Let's see what will happen now. First, a greet call will happen. My hello will come. SayHello call will happen. HelloRohit will come. Then, as soon as getNumber calls, Our flow of control will enter the get number. It will return 10. Who will it return? It will return whatever will happen on the left side of the get number. Now there is nothing on the left side. So it will be lost. Basically, our 10 is lost and nothing will happen to us. It means that 10 will not print on our screen. It is a simple thing because we have not printed. So our hello and hello Rohit got printed and we lost it. So we can do this. So here we passed an argument, here 0 argument and here 0 argument. Now finally we call multiply which will take two arguments a and b. We call these parameters here and we will give the argument there. And it will return a into b i.e. it will return someone's multiplication. So let's say I do it again system.out.println. This time what I do is I get two numbers. So, I will call multiply here. And what are those two numbers? Let's say I pass the number here. So, I write 2, 4. So, I have to multiply 2 and 4. So, what did my VS code do? It wrote A2 and B4. And what was AB? AB is the two parameters that we were expecting. So here I have given multiply 2,4. Now whenever I write this statement, these two 2,4 will go here. Our flow of control will come on multiply. It will assign 2 to A. It will assign 4 to B. It will return 2 into 4, which is 8. So our screen output will be 8. Our fourth output will be 8. So let's see. First hello, then hello Rohit, and then 8. Okay, not fourth, third. Because we are not doing anything with the get number. We could have printed it, but we couldn't. So, you can print it again if you want. So, let's say I printed it. Now, our Charo output will come. Something like this. So, I hope you have understood what are the functions and how we make them and how we call them. Now, think about something interesting. Till now, the code we were writing in this code block, you read its name, main. Isn't that also a function? See, the name of the function is main. I opened brackets and gave an argument. And see what argument I gave. I gave an argument of a string type array whose name is args. So now you must have learned to read it. First of all, the name of the function is main. Here I gave an argument. In the argument, I gave an array. I made an array of string type. And the name of the argument is args. And I started the code block from here to here. Okay. Now I said that every function has a return type. But do we ever write some code here? So finally we write something like this. Return. We don't write anything like return to, return for, right? And you know that if the return type of the function is void, here we have written void, which means it doesn't return anything. So here if we want, we can write a return statement in the last. Or we can not write it. Now see I have written it. Even then our code will run. See. Our code will run normally. There is no problem. This return statement is optional. We can call it optional. Now this is our void main. And static, you know, we write it. We will understand later why we write it. You ignore it for a while. But you think, static void main, this is our static keyword. Void is our return type. Main is the name of the function. These are our parameters. String ergs. And the body of the function that we have in our function is finished. We are doing something and finally our function is finished. So this made us understand that the code that we were writing inside was also a function. Right? So if we understand this carefully, then behind the scene something like this would happen. That JVM would always call our main function. Because you know, if we don't write anything in this main function. Let's say I left this main function empty. See, I removed everything. So even though I have made a lot of different functions below. But I am not calling these functions. I did not call these functions in the main. So if you will see. So see, we didn't get any output, it's empty. So this means that if I don't call my functions in the main class, then we won't get any output. So basically what happens is that when JVM writes and compiles the code, and when JVM starts running it, then JVM will first look for the main function. The function which is main will execute it line by line and finally return it. Now think, in that line by line execution, there will be calls of other functions in between. So let's say one function can call another function. So main called greet, main called say hello, main called get numbers and main called multiply. So as it called greet, our flow of control jumped on greet. Hello was printed, return, again flow of control came here, again he called hello, our flow of control jumped here, he printed hello plus name, i.e. hello Rohit, and returned it back here, then he called some get number on the screen, then he called this, and finally he also returned our main function. And this is how our code flow will run. So now you must have understood this part as well. Now this public class gem is left for us as a boiler plate. But you understood that in java, in which we write code, there is a function which we call main function. And this is the most important function because it is the entry point of any java program. If a Java program has to be executed line by line, then it will start with the main function. And whatever you write in the main function will be executed. Apart from that, if you are not writing anything in the main, like we have written all this outside the main, but we are calling it in the main. If I will not call any function in the main, then it will not be executed. Right? So I hope these things will be clear to you. Rest, when we understand it in depth, Why do we have to write everything in functions in Java? When do we call functions as methods? And when is this a class? So you will understand a lot of things. But now you must have got an idea that in Java, we were writing every code in a main function. Now we have started making new functions of our own. But if we want to use those functions, we have to call them and they can also be called in the main. Right? Now let's move ahead. I have made a new file demo2. java. Because we have already done a lot of work here. I have to explain something new to you. So I have made it an empty file. This is our main function. You must be wondering why main function takes a string argument. We will discuss this later. But now you have got clarity that main function is also a function. And its return type is void. Everything else is clear to you. Now we are going to talk about a thing which we call function overloading. I have written the spelling of function wrong. So function overloading. Now what is function overloading? Look, understand this carefully. Let's say I am telling you, I don't know, To add two numbers, we need to make a function which we had already made. So first we make a function. We used to write static and return type int. We used to write function name sum and I used to take two numbers int a, int b and finally what I used to do, let's say I return a plus b. I can do this, you know. Let's give a gap. And this is done. So this function adds two numbers. And how do I use it? Let's say I take a variable x equal to, I call sum and I can give some numbers here. Let's say 2,3. And I can print this x. Out.println And you know that I can do both of these things in one line. But now I am doing it separately. So our 2 and 3, i.e. 5 should be our output. See, 5 came on the output. Very good. Okay. Okay, now you know that I can call this function anytime. Now I can call it again. Let's say int y equal to, I called sum again. This time I gave 5, 6. And this time I again gave system. Let's say out.println. What did we print? We printed y. Very good. So first my output will be 2, 3, 5. Now 5, 6, 11 will come. So now I have 11. Now I can use it in the code again and again. Same line, sum, now I have to do some more sum tomorrow. Let's say I have to do 10, 20. So I can do this too. I can do anything. How many times can I use it? 67, 45. How many times can I call sum, sum? Very good. But now you think that suppose I have to add three numbers to each other. So I don't have any function. It is made like this. There is a function to add two numbers. A and B. But I don't have any function to add three numbers. And can I use this to add three numbers? No. I have to do something like this. If I want to do it. Let's say I made an int z. Equal to what does that do? Sum call for two numbers. Let's say 3 comma 4. And what does Mac do? Plus, whatever it returns, it adds 5 to it. Right? Meaning, first we summed 3, 4. Called this function. Not 3, 4. Added 3, 4. 4, 3. Our 7 is done. Returned 7 here. On this 7, we added 5 more. And 7 plus 5 is returned to z. So, our 12th answer is on z. We can do something like this. But this is a wrong way. If I want to add 4, I will write here. I work better than this. I add one more function. Okay? So I will make one more function. Let's say static int. And what should I name it? Let's say I name it sum2. It takes three numbers. Int a, int b, int c. And it can add these three. Let's say it can add a plus b plus c. Something like this. Right? Now, Now when I want, I will directly make int z and I will call my sum 2 by giving 3 numbers. Let's say 3, 5, 6. Something like this. So 9 plus 5 becomes 14. We should get 14. Now first we will print this. System.out.println Sorry, println z. We didn't print this. So we printed x, y and z. 2 plus 3, 5 plus 6. And one more function is 3 plus 5 plus 6. So, our output is 14. Very good. This is what we expected. Now, what does function overloading mean? Function overloading means that when you make such functions, which are doing almost the same work, like this is adding 2 numbers, this is adding 3 numbers, right? So, you can also name the function the same. Like this. error. That means compiler is treating these two numbers, these two functions separately. Here there is an error because I have called sum2, sum2 is nothing, now I will have to call sum. Okay? Now you must have a question in your mind that how the compiler will know when I write here that I have to call sum. Like here I have written that I have to call sum. So it should call this one. And here when I have written that I have to call sum, it should call this one. It's a simple thing. Apply your first thought principle. Argument is different, it takes 2, a, b. It takes 3 parameters, a, b, and c. So if we give 5, 6, 2 pair arguments, then this call will happen. If we give 3 arguments, then this call will happen. Okay? This is just a way that you can overload same functions in Java. Overload means, you understood that I can use it again and again. But the condition is that some things should be different. First, what is this? This number of parameters should be different. There are 2 parameters in this. There are 3 parameters in this. So this is the only way. There is another way to change the type of the parameter. Let's say I make another static int sum. And what should I do? I was taking int a and int b here. I was taking 3 here. If I take 4 here, it will be different. But I take two variables, double a comma double b. Double b, something like this. So what did I do? I took integer a b and double a b here. Now I have taken two variables of double type. So what can I do? I can return. A plus B. But now see, there will be an error. What will the error say? The error will tell me that the type mismatch cannot convert from double to int. Now I will have to convert double to integer. So you know that I will have to do type casting of this A and then of this B. Or let's do one thing. We will do the entire type casting of this. First I will add A and B. Okay. Now it will work. So see this is also valid. So I have given three functions with the same name sum sum sum. One takes a comma b of integer. One takes a, b, c. So the number of parameters are different. I can write in this. I can write in this. Different number of parameters. Number of parameters. And what is here? Sorry. What is here? Our different Types of parameters. So, this also comes in function overloading. Okay. Either I separate number of parameters. A, B, C, A, B. Separate type of parameter. Okay. And let's do one more thing. Let's say. I. Static. Int. Sum. I make one more sum. This time I can do. I can change the flow of parameter. I can change the order. Okay. But here are two integers. I work for that. Three overloading of sum. I make one more function. Let's say. Static. Int. let's say greet this greet function let's say it's called void function, simple it's not integer type, it's void type, void greet it takes two arguments, one argument takes string name and one argument takes int age let's say string age and int age and what does it do? system.out.println, it prints something on the screen and what does it print? let's say it prints hi plus we will concatenate the name again plus we will concatenate full stop your age is plus sorry here will not be plus here will be space we will concatenate age so what will be printed here? if I will write aditya in name and give my age let's say 28 so here will be hi aditya your age is 28 something like this now think I can make one more function static void grid name In which I will change the ordering of its argument. First I will take age. And then I will take name. So this is also valid. See. Here also no compiler error has come. Now here also I can do the same thing. Let's say. Same thing. System.order.container. Here I will do the same. Hi name. Your age is your age. Okay. See. No compiler error has come. Now in this case how the compiler knows. That these two methods are different. It is absolutely simple. If I. In arguments. These are parameters. If I call here. Let's say. Now here xyz is done, this was void type, it doesn't return anything So if I simply call greet, now the compiler is saying that we have to give arguments for the function Now which greet do I want to call? Let's say this one So first I have to give a string and then I have to give age So first I gave string aditya and then let's say age 28 So now as I gave this, so first it selected name and then age First name and then age. This function will be called. Compiler got to know before itself. Because first I gave a string. How do you know? I gave it in double quotes. So it became a string. Second I gave an integer. So it became an integer. Now if I call greet again. But this time first I give age 28. This time let's say name is Rohit. So what will happen this time? First the age and then the name will appear. So what can I do? I have told you. I can change the number of arguments. Different types of parameters. And what can I write here? Let's say I can change the ordering of parameters. So here I can change the ordering of parameters as well. Very good. So we have made three sum functions and two greet functions. Okay. Can I do one more thing? If I make one more function. Let's say this time I am making a static example. I will make one void. Function name is let's say fun. Okay. It doesn't take any argument. And this is just system.out.println. Sorry. System.out.println. Hello. Let's just do this. Okay. Okay. Now let's say I make another function, static, this time I just change its return type, this is void, this time I make it int, the name of the function will be the same, fun, and let's say it doesn't do anything, it will also print hello first, and I have to return something, so let's say I return 5, I have to return an integer, so I simply return 5 like this, so see you will see a compiler error here, once that error is read, Duplicate method fun in type demo. In demo 2, our duplicate method is fun. Why is it telling duplicate method? It hasn't told it yet. Here, it hasn't called sum duplicate. It hasn't called our greet method duplicate. But it's telling fun duplicate. Why? Because our compiler I am not able to decide which function I have to call Because there are two functions whose name is fun But both of them have different return types But we have done everything till now We have separated the number of parameters Then it got to know If we have separated the type of parameter Then it got to know If we have separated the order Then also it got to know But why is it not able to know if the return type is different? If I had written void here Then it would have become very similar Here void return type void return type Here too the function name is fun, here too the function name is fun. Here too there is no parameter, here too there is no parameter. Now the compiler can get confused. That whenever you call fun, basically whenever I write fun up here. Okay, now I don't want to give any argument here. So as soon as I write fun, then which function will the compiler call? Now he is confused. But if I am returning integer here, then why is he confused? Why can't he see it as return type? It has a simple answer, that's why he can't see it. Because I told you, Whenever we call a function, this function, whatever this function is returning, if we want to take it in a variable, let's say a, this is optional. If I want, I should not collect it in any variable, let that value be lost. So compiler will say, if you have written something like this in code, whether you write it or not, but if you write it like this, then how will I know that Which function I have to call Because you have simply written fun Now what if you are expecting That I should call this one And whatever integer I return You should not collect it in anyone You want it to get lost Or what if you are expecting That I should call the void one But I don't know Whom I have to call So what I will do is I will simply throw compiler error I will not let you do this Because I don't know Which function you want to execute Okay So compiler error Are those errors Which compiler already tells you That I don't understand I'll get confused later, so fix it now. So the way to fix it is either I change its name, now it's fixed. And if I don't want to change the name, then I play with these parameters. Either I don't take a parameter in this, I take a parameter in this, let's say string name, then it will be different. I can separate the types of parameters or I can separate the order of parameters. But I can't decide this on the basis of return type, so let's say I call it fun2. Something like this. Now, as I go up, I called fun, now there is no error, because now I understood, if I simply fun to call, sorry, when I called fun, then it knows, simply, there is only one method, sorry, there is only one function, whose name is fun. So, it will execute it, and hello will come on our screen. Okay? Come on, I hope you must have understood this. Look, now let's say, we see something, whose name is chaining of functions. Chaining of functions. What does it mean? This means that I can call one function to another function to another function to another function in this way. Let's assume that I have a void main function on the top. I am not writing the full name of this function. It has string, ergs, etc. Let's say I call a function whose name will be greed. Or let's say a very simple name. Let's make it smaller and finish the code block. Now I called a function from this void main function. Your flow of control must have gone inside that function. Let's say it is a void function a. No parameter is taken And inside it calls another function b Then what will be our flow of control? It will go inside this b Something like this And this is also our empty function b Which does nothing It calls a function c from inside So again the same story will be repeated This is also our function Which we will make it a little smaller This is also a function that will call another function and so on. Here we can do void d and it can keep running. This story can keep running. So this is called chaining of functions. Chaining. You see, one function is calling the other, the other is calling the third, the third can call the fourth and so on. Now, how will you go back in this? So let's say the chain is till here. Let's assume the chain is of three functions. And let's assume that there is no C call here. I will remove this for now. There is no C call here. Let's say something is being printed here. System.out.println Something is being printed here. Let's say hello. Something like this. So now let's see how our flow will be. And suppose nothing is written outside this line and nothing is written below this line. So our flow will be something like this. First of all, it will execute the main function. Then it will go to the function of A. Then it will see that it has started executing A. First of all, it got B from the line. If it wants to call B, then it will go to B. Then it started executing B. It said that it wants to print on the screen. It will print hello on the screen. As soon as it printed hello on the screen, it said that the function is over. Now I have to go back to where I came from. It came from here, so it came back here Now it had executed B, it will try to execute the line below it Now there is nothing below, this is also finished So it will say that I also have to go back from where I came, it went back here Now it had executed A, that's why it went down If we try to execute something below a, we will not get anything. If this is also finished, then it will also come out from here and our whole program will be run. Now what we call this, that after one, a function will be executed and then they will go back one by one. We will read this in depth when we will read about memory management and we will read what is stack memory. We saw this in the last class, if you remember. When we said that the reference variables of the arrays are stored in the stack, right? So, we will read all that later. But now, understand the flow of control. That first, we will jump from main to A, from A to B. And after executing B, we will go back to A, then back to main, and then back out. So this is our function chaining. You can execute it in code as well. You call an A function, then call its B, then print something in it and see. And see how that flow will call. Okay. What happens is, if I print something below this B, then let's say we have written something here, system.out.println, and here instead of hello, I have written hi. And same, I have also printed something below this A. Here also I have written system.out.println, and here I have printed hello, hi, and bye. I have written something here, bye. So how will things be printed on our screen? Now you can understand, first our flow of control came on A. He said that I want to execute A, so he stopped here, that I will go below this later, first I will go to A to execute. He entered A. He saw the first line B, so he wants to execute B. So I will go below this later, first I will go to B to execute. Then he came to B to execute. Then he came to B, he saw that he wants to print hello on the screen, so first hello came on the screen. Hello is printed, the function is finished, it will go back to the caller. We are telling it to call, it is our caller. Now it has executed till B. Below B, it will see what is high. It printed on the high screen. There is nothing below high, so it will go back to the caller who called it. It has executed till A, it came below A, what is below A? On the screen we have buy and below buy nothing is there. So on the screen we have hello, hi and buy. So basically the flow in which we had written, it came in the opposite flow. So let's quickly do this too. So see what we are doing here, we are working on chaining of functions. Okay, so how can we do it? So what did I say? Let's say I call a function in my void main whose name is let's say fun1. I have kept some random name and I print something below it. System.out.println. Let's say bye. Okay. Now let's make this fun1. So what will be our fun1? static void fun1 because it is a return type void. That's why it is not collecting any value. fun1 and it will not take any argument. And let's say it calls some function fun2. And after calling the function, it prints something on the screen. Let's say, hi. Okay. Now I have called fun2 here. Now there is an error because no function of the name fun2 exists. So let me do one thing. Static void fun2 Now it calls a function fun3 And it prints system.out.println So hi, hello And let's say I make a function fun3 Static void fun3 It doesn't do anything, it just prints println Okay, so let's see, what will it print? Hello, Hi, Bye How are you? Let's say something like this And it returns from here So see how we did the function chaining First fun1 will call, then fun1 will go to our flow of control Then fun2 will call, then fun2 will go, then fun3 will call, then fun3 will go So main will call fun1, fun1 will call fun2, fun2 will call fun3 Fun3 printed on the screen how are you So first of all how are you will come Then we will go back here, then hello will come Then we will go back to fun2, then hi will come And then we will finally go back to main And this was executed, then our bye will come So our flow will be how are you, hello, hi and bye This is how our output should be. So let's quickly see. How are you, hello, hi, and bye. So you must have understood the function chaining. Now we have to see one more thing. So what did I do? I made a class called demo4. And we have to see scope. of a variable. This is also very important. And when we are reading functions, we should definitely talk about it. What is the scope of a variable? Till now, you must remember that we were declaring variables. Here, I have written my main method. We used to declare, let's say, int x equal to 4, int, let's say, y equal to 5, something like this. And we could print these variables, system.out.println. Let's say, I printed x, space, or let's say, comma, and then what did I do? I printed y. y. Plus y. Okay. So, my x and y will be printed as 4 comma 5. Very good. Yes, isn't it? We have already done this. Now, if I write a function below this. Let's say, I write a function. Static. I write a void function. Simply. And I have written the name of the function as fun. I don't take any argument. And you know what I want to do here? Here, the spelling of static is a bit wrong. Static. I again want to print this x comma y. So, can I do this? Look, I have written x and y here. And now you will say that you didn't call fun. No problem, I will call fun as well. So what will happen first? First our flow of control will assign x as 4 and y as 5. Then it will come here and print 4,5 on the screen. And then it will go to the function of fun. In the function of fun, I tried to print x and y again. But there is some error. X cannot be resolved to a variable. What does this mean? In fact, this is also correct for y. If I hover over for y, then y cannot be resolved to a variable. It is not able to recognize x and y. What is this? Where did this variable come from? You have never declared this. In Java, whatever you write in a curly bracket, whatever you write in a code block, whether you write a function, see this is a function, whether you write an if statement, we put a curly bracket in it, whether you write a for loop, we put a curly bracket in it. Whatever you write in the curly bracket, whatever variable you declare in the curly bracket, the scope of that variable is till there. That means, this 4, 5, that is, x and y, the scope of these variables, that means, how visible they will be, they will be visible till inside this main function. Right? They will not be accessible to the fun. Unless and until I pass it in the argument. I can do something like this, x, y, and collect it here. Let's say, int x, int y. I have collected it from here. So, I can do this. Now, see, my error is resolved. Because I have given them here that you take 4,5 as an argument. Here, I have collected it as a parameter. That's a different thing. But if I am not doing anything like this, if I am not doing anything like this, In this way, it will not recognize my x and y. Here, I have to give some value to x and y again. Let's say, I gave 4 here again. And here, I gave 5 to y. So, I have to do it again. That means, this x and this y are different from this x and this y. This is called scope of a variable. And we call these scopes as local scope. You know what local means. It is accessible in the local code block. Its scope is also local. We call it as local scope. Now you would have understood if I write an if statement. Let's say I write any condition. If x is equal to 4, then it will execute. And here I will write a variable. variable. Let's say int j is equal to 7. Something like this. So you know this j is only accessible in this if. So if you see. I write system.out.println. Here. And try to print j. Then I will get an error. See this. J cannot be resolved to a variable. Because it doesn't know what J is. Because we have written J in the scope. But I can write it here. See here, there is no error, because it knows what j is, because in this code block j is 7 If I want to define j here, then I will have to make a new j, j equal to 7 So this j and this j are different, so as soon as this if block is finished, let's assume this j is also finished So whatever variable we have declared in a code block, so as soon as that code block is finished, that variable also gets finished by memory Now there is no existence of this j, it is finished by memory Now we have to make a new j if we want to use it in the main. Okay, so I hope you understood this too. You can do the same in for loop. In for loop, while loop, in any loop or in any code block, whatever variable you declare, its scope will only work till the time that block has a scope. For example, if we finish a block, if we finish an if statement, if we finish a loop, then that variable will also be erased from memory. Now it has no existence. So, till now we have studied a local scope. There are variables of a global scope that everyone can use. If you create a variable below classDemo4, let's say int stringName and define it as aditya. So I have created a variable named name aditya. The scope of this variable is global. Global scope means that I can access this name anywhere in this whole file. I can also say here that if I print it here quickly, I can print the name here too. What's the error? Cannot make static reference to not. Okay, when you make a global variable, you will have to write it here static. We will discuss that later. Why? But now write static here. Now see, I can use the name here too. I can use the name here too. See, the whole function is different. I do it even then. Right? So, there is no error here and there is no error here. Okay? So, this is called global scope. This variable is global. Globally available. And do you know when this variable will remain? Till the time we work in this whole file. Meaning, if I say in another language, till the time our class remains. Okay? Now, what is class? We have not studied that yet. But till now, you have understood two things. What is global variable and what is local variable? Okay? So, remember this too. Okay? Now we have one last thing left. So let's go to the screen and see that. So we have seen chaining of functions till now. If you go ahead with this concept, then we can talk about another thing which we call recursion. And this is a very interesting concept. Almost every programming language has recursion. What does recursion mean? Let's first understand that. So if we go from the meaning, then recursion means a function. which call itself. What does it mean? A function that calls itself. I had told you that we can train a function. That it can call a, b, b, c and so on. What if a function calls itself? This is also possible in Java. Why is it possible? There are many reasons. In fact, recursion is used a lot to solve complicated problems. Now you will know how. So first understand the definition. A function which calls itself is called recursion. Now for example, suppose our main function was here. Here string argument was written. and here let's say I call a function a. Okay. Now suppose this a, I'll show it like this. This is my main. Main called a. Now here it'll be written void a. And it won't take any argument. Let's say it calls A again. See how our flow of control will jump. I'll make it in brackets first. So flow of control will go like this. First it'll come to main and call A. Okay, I'll enter A. It came to A. Now it saw that it has to call A again. Okay, I'll go up again. Then it'll execute A. Okay, it has to call A again. Then I'll go up again. Now it'll keep moving in this loop. It'll keep executing A again and again. And we'll never be able to get out of this A. But why did we make it? If we got stuck in an infinite loop, then why did we make it? We made it because first of all, it's a very good use case for recursion. Secondly, we never make recursion like this. We always make recursion with a base case. With a base case. Base case means a case from where we can come out of the recursion. Okay? Okay. So, how will we make this? Let's see this once. So, look. First of all, we solve a problem using recursion. You will be able to understand better. So, the first problem is that I have to print numbers from 1 to 10 on the screen. You have done this before, right? What have you done? If you run a loop, for loop, i equal to, let's say, from 1, and run it till 10, less than equal to 10, i + +, and every time you do this, a system.output.println, i, from 1 to 10, numbers will be printed. We have done this before. But we did this iteratively, by running a for loop. I can do this through recursion too. How? Let's say, I have written a function in main, whose name is print, numbers. Print num. Okay. I will write it together. Print num. Something like this. And what does it do? It expects an argument. You give any argument here. Let's say you gave n and here you have declared int n equal to some variable. Let's say 5. So it will print 1 to 5. Okay. Very good. Now we have to define this print num. How does this print num work? This function. Okay. So I will make it here. So let's say my function's name is. Let's Write it in white. Yes, the name of the function is printNumber. So, I have to give a return type of this. So, I will also give that. Let's say printNumber does what? Void return type. It doesn't return anything. It simply prints the number. printNumber. Now, what will it take in the parameter? Let's say an integer will take n. Number. Isn't it? And in this, I have to print 1 to 5 or whatever number n comes. I have to print 1 to n. Okay. So, let's see what I can do. I work. First of all, let's make it a little bigger. First of all, I write print number n minus 1. See, I called recursive call here. This is recursive call. Recursive call. I called the same function again. By reducing one parameter. Here was n, here I called n minus 1. I said print n minus 1. Now I will tell you how to do it. After that, I wrote system dot out.println n and print the nth number and here I have written a base case if n is equal equal 0 return Now you might not be able to understand this code at all, what we are doing here. But let's understand one by one, you will be able to understand things very well. Now you have to look at this function carefully. I will remove this recursive call, you must have understood that it is a recursive call. Now just look at this function carefully. I have kept it on the left side. Now see how it will be executed here. We will run dry, let's go slowly. First of all, our print name function will be executed. Assume that print number will be executed from the main. And here we assume that we have got the value of n, 5. Now let's consider a small number, we have got the value of n, 5. Okay, very good. So first of all, our flow of control will go in this line. Let me mark this too. First of all, our flow of control will go, let's remove this, this is nothing. Yes, first of all, our flow of control will go in this line. If n equal equal 0, then our test will be, what is our n? Is it 5? Is 5 equal to 0? No, it is not. If it is not equal to 0, then we will not return. We will go down from here. So, our next line will execute. We will come here. So, till now, we have come here. Now, what did we do? We called this print number again. So, first of all, our print number was called. Print number was called. 4, 5, here I am writing the value of n directly that for whom it was called, for 5 it was called. Now as soon as this print number was called for 5, what did it do? By coming on this line, by coming on this line, it called this number again by giving n-1 which means for 4. So here I am making a chain, print number 5 was called, after that what did it call? It called the print number again. For 4. Okay. Called again. Means our flow of control will come again in the first line. We will again check if n is equal to 0. What is 4? Now what is the value of n here? Here the value of n is 4. Okay. Is 4 equal to 0? Not at all. So we will move forward again. Then what will we do? We will call print num again for 4 minus 1, 3. Means what happened again? Print num called. Print num call is done, for what? for 3. Very good. Now here 3 is done. Now when it will run again for 3, we will test whether 3 is equal to 0 or not. We will come down and call print num again for 3-1, 2. So let's write that too. So now you must be understanding how we are running. What did we do this time? We called print num for 2. Then if you see, here the number is 2. Is 2 equal to 0? Not at all. Then we will go down and call print num. for 1 so let's do this too we will come down and do print num for 1 now here 2 is 1 is 1 equal to 0? not at all we will call print num for 1 minus 1 for 0 so we will come here so see our chaining was being made Now print num call is done for 0. Now, as soon as 0 is called, here it is 0. Now, here it will be a chest. Is 0 equal to 0? Now, this condition is true. As soon as it is true, we have written return. This function is void function. So, we can simply write return. Return means, now you can assume that it was in the curly bracket of if. And you know that if there is only one statement, then it is curly bracket and void can also be done. So, I had written directly here. So, yes, n equal to 0. So, we have written return. Now, from where will it return? It will return from here. From here. It will return from here. To 1. Why? Because 1, 0, this function, print num 0, through whom was it called? It was called through this. This is very similar. We remember we talked about chaining above, here. Here, A called B, B called C, here C was not there. B called and directly printed something. Then we went up one by one, right? We called that chaining. Same here is a chain of similar functions. So if you see here, print num 5 called print num 4, print num 4 called print num 3, print num 2 called print num 2, print num 1 called print num 1, and finally print num 0. So one function kept calling itself but with different arguments. First 5, then 4, then 3, then 2, then 1, then 0. Now when we will go back, because the zero function is finished. Now as soon as we come here, n equal equal zero return. We will return from here. We will not go down to execute anything. Because whenever we see return, our flow ends there. Okay. So now where will we go back? We will go back to one. Because we came from here. Now as we go to 1, our flow of control will come till here. It stopped till here. Now here it went to print 1-1-0. Now as it came till here, now it will execute the statement ahead of it. You know this too, we had done it above. First what it used to print? Hello. Then it used to go back. Then because it called from B, then it used to print the statement below as Hi. Then it used to go back. Now it called from A, then it used to print the statement below as Bye. So similarly it will happen here too. It was executed till here. If you can see it properly. Let's zoom in for a second. Okay. Yes. So it was executed till here. Print number n-1. That means here. 1-1 was 0. But this statement was not executed. So now it executed this statement. System.out.println n. What is n here? It is 1. See, first it went for 0. Now it returned from 0. So it came back here. Here we gave argument 1. So it printed n on the screen. That means 1. So I am writing our output here, let's say 1 came on the screen Very good. Let's make it a little lower. Okay. Then, what will happen? Now, as soon as we write 1 here, it got finished. So, this one also returned to print num 2. Okay. Now, print num 2 was also executed here. After that, it wrote the line system.out.println n. Now, here n's value is 2. So, 2 got printed on the screen. And then, this also returned after 3. Now, you must have understood what the flow is going on. Now, what 3 must have done? It must have printed its last line here. system.out.println n. n here is 3 so on the screen it will be 3 3 sorry I will write it again 3 will be on the screen And it will return 4 Now again, what will 4 do? 4 will print 4 And it will go back after 5 What will 5 do? 5 will print 5 Okay, now 5 was final As soon as 5 was printed 5 saw that now my code is finished Now it came out from 5 too And where did it come out from 5? This print num 5, which is the first method Who must have called this? Our main function must have called this So here I will write main somewhere Main must have called this. Now it went back to main. Let's say main is finished. So what output came on our screen? Output came from 1 to 5. 1, 2, 3, 4, 5. So we printed 5 numbers. Now if I had to print 1, 2, 10. So I would have given starting argument 10. So, this chaining would go till 10. First, it would call 10, 9, then 8, 7, 6, 5, 4, 3, 2, 1. And then it would go up one by one. And what would it do? It would print the line with n below. So, see, what we have written here, n equal equal 0, we call this line, if I write it carefully, Let's write it again clearly. So now look at this function again, I have written it clearly. Now what I was saying is that this part I have written, this part, this part, I call this our base case. So what is the use of base case? Base case is used to stop the recursion. So if you think, I don't write this, it doesn't happen in our code. So what would happen? If I call 5, 4, then 3, 2, 1, 0, and nothing happens at 0, then it doesn't stop. If I call 0, then I call minus 1, then minus 2, then minus 3, and it keeps running infinitely. It never stops, right? So what is base case? Base case means a case that will stop the recursion. That the recursive call can come up to here, after that we won't let it go down, after that we just have to go up. So see, what happened here? This was the base case, 0 You can come till here, but you can't go below this After this, you will start going up again from where you came So like stairs, you will come down one by one and then you will go up one by one So this is our base staircase, there is nothing below it, it is a floor You can't go underground because this is our base case So understand it like this So this is our base case, okay So then what do we call this? This which we have written, print num n-1 Now you must have understood, we call this our recursive call Recursive call And finally, we printed it. So, I hope you must have got the idea of recursion. What does recursion do? It calls its own method again and again. So, printnum can call itself. printnum n-1. Where do we use it? We use it for these things. Such problems, which we express recursively or can do. Like, we want to print 1 to 10. So, what will I do? I will go down recursively and then I will go up again and I will come back after printing 1 to 10. But you will say, this is complicated. I could have used a simple for loop but I did it like this. Why? See, recursion becomes very useful when problems are so complicated that you can't solve them normally. Normally means you can't solve them iteratively by applying loops. You can do this but it will become a little complicated. Your recursion becomes very useful there. When you solve problem solving, data structure and algorithm questions, then you will see that recursion is very useful. In fact, let me tell you, if you were very good, if you... If you want to visualize recursion, then I have explained to you how recursive calls go. It is chaining and then it goes back up. But this creates some complications in your mind that how will it go back up. So to understand this well, you have to believe that you don't have to go inside, just explain to me what is the work of one layer below and then I will handle the rest of the work myself. How? Let's see this once, you will find it very interesting. So let's say I tell you to write a code that will find the number of 5. Do you know what 5 is? First the number is 1, then 1, then 2, then 2 plus 1 is 3, 2 and 3 is 5, 5 and 3 is 8. If the pattern continues in this way, then we call this sequence the Fibonacci series. Fibonacci series. Okay. Fibonacci means. Sum up your last two. And write further. Then sum up five and three. And write further. Next number will be. Eight plus five. Okay. So write further. Keep running the pattern like this. This series is called. Fibonacci series. So if I tell you. You have to write a code. That I don't know. Nth. Find the number of 5th and give it to me. So what will happen? For example, this is my 0th 5th number and this is my 1st 5th number. This is 2nd, this is 3rd, this is 4th, this is 5th. I am writing its iterations. So this is my 5th number. So if I give the value of n, n is 4. So if it is 4th 5th number, I will get 5. If n is 5, 5th 5th number, I will get 8. You have to write code in this way. So how will you write? Now, what will you have to do to write its iterative code? You will have to go from the starting. You will have to store two numbers. Then you will use arrays in arrays. You can write it yourself and see. But let me tell you, if I express it recursively, then look, mathematically, I cannot express it like this. That I want Fibonacci of n. Right? So what is Fibonacci of n? Can't we express it like this? Fibonacci of n minus 1. plus Fibonacci of n-2. I can't express it like this. Right? Okay? So if you think mathematically, I'm not going into computer science right now. I'm saying there's a function Fibonacci of n. To get that out, can't I express it like this? That I first get out n-1, get out n-2, and add both of them. Right? So now how will I write it in code? You'll do exactly the same. I'll make a function Fibonacci of n. Okay? It can have any return type. Let's say int. Right? Now, what will I do in this? I will say that first of all, I have to find fibonacci of n. Let's do one thing. You think like this. Don't think in recursion. What will I do to find fibonacci of n? First of all, I will find fibonacci of n-1. Let's say in a variable x. Fibonacci of n-1. So, I called the function of fibonacci. I called that function again by giving n-1. I will assume that I got it. Then, I will take out one more int y which is phi boneki of n minus 2. So I got n minus 1 as well as n minus 2. And finally I will return x plus y. Because x plus is x and this is y. What will this give me? Phi boneki of n. So in phi boneki of n I will return x plus y. And this is my work. Now only one thing is left. This is base case. Here I have to write base case. So the base case should be that if the value of n is 0, look at this. In the Fibonacci series, if the value of n is 0, this is n and this is Fibonacci. If the value of n is 0, then Fibonacci is 1. If it is 1, then Fibonacci is still 1. So I can make this base case that if the value of n is 0 and n is 1, then we always have to return 1. This is our base case. Now try to make a recursive call for this. You will understand how this code can print any 5 of n. Think about it. How did you imagine it? Give me n-1, give me n-1, give me n-2. I will add them and return them. We didn't write any other logic. Now recursion will automatically keep doing it inside. So see how will it do recursion? If I make it small and show you, then it will work like this. So let's say you get 5. For a Fibonacci of 4 and for a Fibonacci of 3. See, there are two recursion calls here. Fibonacci n-1 and Fibonacci n-2. Now, who will call the same Fibonacci of 4? Fibonacci of 3 and Fibonacci of 2 will be called. Similarly, who will call Fibonacci of 3? Fibonacci of 2 and Fibonacci of 1 will be called. Now, who will call 3 here? Fibonacci of 2 and Fibonacci of 1. And who will call 2? 1 and 0 will be called. How will it call 2 here as well? It will call 1 and 0. Now you think, this fibonacci of 2, again, this too will call fibonacci of 1 and fibonacci of 0. You understand that recursive call has become like a tree. Now you see, this 1, this 0, this 1, this 1, this 0, all these in leaf, 0 and this 1, all these are base cases. What are all these? Base case. I am going towards data structure and algorithm, if you haven't solved any question till now, then you might find it a bit complicated. But if you learn to practice recursion, then everything will be clear to you. So see, how will our program run? As our flow of control is, first it will try to print 5 to the power of n. So first it will come to this line, 5 to the power of n minus 1. So as 5 to the power of 5, so first it came here, 4. We know the answer of 1, which is 1. This is the base case. If n equal equal 0 and n equal equal 1, return 1. So, this will return 1. So, I can write it like this that this will return our 1. Now, as it returned 1, this line will end here. Then, it will go to the next line. 5 minus 1 of n minus 2. So, it will go to print here. Now, what will 5 minus 1 of 0 return? 1. So, it also returned 1. So, it returned 1. It returned 1. So this line and this line will return x plus y. So here 1 plus 1 is 2. So it finally returned 2. Now the same, it returned 2 on the left side. So it returned 2 on the left side. Now again it will go to the right side, on the side of n minus 2. It went here. So it returned 5 times 1, which means again 1. So it returned 2. It returned 1. What is the total? 3. Something like this. Now, it will move upwards. If you still find it a bit complicated, then I would say, you should first do easy questions of recursion. Like, first print 1 to 10. Print 10 to 1. Print it upside down. I will give you some more questions to practice recursion. Now all these concepts go into some data structures and algorithms. If you want to practice algorithm then you can solve any question on GFG, Leadcode or on our Strike platform. So all this comes from practice. When you do DSA then you will practice it. But you understood the logic of recursion. You have a function. You call that function again by changing any parameter. So, you have made your parameter n as n-1 or n-2. Why do you do this? You do this because when you use chaining, let's say we did it in this. then a very complicated problem is easily solved. Now this is a complicated problem. But this was an easy problem. How did we do it? Yes, when we printed 1 to 10, this one. This was an easy problem. And I think you must have understood this well. Because you understood chaining. 5 called 4, 4 called 3, 3 called 2, 2 called 1, and 1 called 0. So how did this chaining happen? You must have understood this. And how did we back propagate this? You must have understood this too. So if you understood this, then you have to apply the same concept in Fibonacci. In this, there was only one recursive call. In Fibonacci, there are two recursive calls at a time. Fibonacci of 5 also calls 4 and 3. Why? Because it is written in the code. Fibonacci of n will also call n-1 and Fibonacci of n-2. So, 1 call for n-1 and 1 call for n-2. So, first a straight line was coming and now we have a whole tree like this. Now, when you go back, then go the same way like chaining. Chalo, iss video mein itna hi. I hope aapko functions samajh aaye hongi. Baaghi jo maini question practice karne ke liye thi hai, please une karna. Aapko recursion ya in general functions kaafi clear hongi. Aapse milte hai next video mein. Tapta ke liye, thank you so much.