Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So now how we write a recursive function for it.
So let's just write down what we are looking for in the comments here and we are going to type that.
We are going to find the sum of one plus two plus three plus.
So on up until a given num.
Right.
So the function will get these num as a parameter and it will return in a recursive manner.
The sum from one up to these given num.
So we know that the function will receive is a parameter or some integer.
Right.
In this case NUM is an integer.
So we know it will be an integer.
And also we know that the sum, a sum of indigenous is also expected to be of an integer type.
So that's why we are using the function type as.
So end.
And let's call this function as we called it previously.
Sum up two.
Okay.
And in the parameters, we are going to specify it now.
So I hope that's clear.
So far we just created our signature for these function.
And knowing the curly brackets, we are going to specify how these recursive function will work to find
out these some.
And we know that the sum of all the numbers from one up to NUM.
We can say that it will be equal to.
We said it previously.
Just a quick reminder to the sum of one plus two plus three plus.
So on right.
Glass num minus one num minus one glass.
And we separated as just num.
OK, so it's the same sum we just separating it.
That are the total something, the bigger problem equals to some smaller prob problem.
Right.
The sum up to num minus one.
All of these, some plus of these given num.
OK.
We just kind of kind of extracted from.
Are it a problem that is less than the given problem that the big problem.
OK.
So we have the big problem.
OK.
This sum from one up to num.
When we divided, as we said previously in the introduction videos, we divided with split it into some
smaller problem right from one up to now minus one plus these given.
Okay.
So we created a little sub problem from these given problem and now we want to see how we can specify.
So basically we know that a recursive function is just the function that calls itself right.
And we know that a function some up to knows how to find the sum up to a given num that it receives
here up to a given num from one up to these given num num.
So we will say that it these function will return.
Right.
It will return num.
Plus we said num plus this sum up to num minus one as you can see here.
So it will return the num plus sum up to num minus one.
Okay.
So we extracted these num, we extracted it and we are using a recursive function call to a sub problem
where we are going to call and find the sum up to this num minus one.
Okay.
So if we are going to run this function right now, let's give it a try.
For example, we will initialize.
We will let.
Let's just declare it so int num.
And we know that we will bring some nice message to the screen.
Enter your number and then these are will insert his number and let's use instead of enter insert your
numbers.
So now we are going to use Skåne F percentage the and relating to now.
And we want to receive the final result from these functions sum up to given num returns a result.
Right.
So we will create another variable and college result.
And here we will call these functions.
So result equals.
To sum up to this given number.
So for example, if the NUM was three, then these function we expected to return six.
Right.
One plus two plus three.
And now let's try to print out the results.
So print f resolved.
Equals two percentage.
Right.
And we are going to just see what the result is.
So let's build and run it.
So build and run and and enter your number.
We are going to insert three, for example.
And let's see what happens.
And basically nothing happens.
And it seems that our program get stack.
OK.
We received some value that we didn't even expect.
Something something went wrong here.
Right.
We expected that there will be just a message with the result equals two six.
And we got something very, very strange here.
And let's.
Since right to understand what was the problem.
So what we are doing here in this line, we are calling this some up to these given Nahm and given here
a number of threes.
So some up to three will return three plus some up to two.
And this recursive call calls another instance of this function with the number two.
So sum up to two returns, two plus sum up to one.
And there you go again and again and again.
And it's kind of an infinity of infinite recursive function call.
There is no stopping condition.
We just will call it for a sum up to zero, sum up to minus one, sum up to minus two and so on.
There is no end to it.
And the function will eventually not return as anything because it will never stop.
It will never reach some stopping condition.
So that's why we are going to use our base case.
OK.
We know that there should be in the recursive function, some base case, some stopping condition.
That tells when we should stop the recursive calls.
And in this case, it will be simply when numb when these given num sum up to where and a num equals
to one because we know that it's straightforward.
That sum up to one is just one.
OK.
And we will use this condition if num if num equals for example equals to one.
Then in this case we know that there is a trivial solution, a trivial resolve that we can return from
this function and it will be return.
Return are just one.
OK.
And I'm not using here if Nahm is less than one, because we we will assume that this number that the
user provides, that this function will be greater or equals to one.
So at this point, we know that there is our stopping condition, as it will when we reach one.
So we will have here three function calls.
So the first function call will be with NUM equals two three.
And the function will return, will return three.
Plus are some sum up to up to two.
Okay.
And then we will call the function sum up to do with num equals due to a right.
Because we want to return the result for this function and we don't know it yet.
So the result of these function will be two plus sum up to sum up to one.
OK.
And also we are going to call this function when NUM equals two one in Iraq, aggressive manner.
And in this case, we will see that the result of these functions sum up to one num equals two one.
And we know that here, if we take a look at our stopping condition, if NUM equals to one, the function
is just going to return one.
So this function for sum up to one will return one.
So instead of this one, we are going kind of backwards, right?
Right now we are going instead of these function, we are going.
Because we found the solution, we are going to specify one.
So for sum up to two, we know the result will be three.
So instead of these two plus one, we are going to specify here three and four, some up to three,
which is this case.
We are going to return six.
And this result is going to be returned.
Hearings that have this line instead of this are function call.
There will be just six.
So the result variable will hold.
Right now, six.
And let's actually see the read exact.
That's what happened on build and run it.
So three, we provided with the number three.
And we see that the result is six.
And if we want to test it out for five.
And we expect for five the do there will be fifteen.
And that's exactly what we can see on the council.
So once again, just to go over this solution.
This function solution, we wanted to write a function that will some of the numbers from one up to
given NUM.
And we wanted to use a recursive methodology, let's say, let's call it like this, and we need a recursive
function call that we'll be able to split a given problem to find the sum up to a given num to some
smaller sub problems.
And we know that some up to a given num is actually the num itself, some number plus of a sum up to
num minus one.
OK, so we know that's the recursive call.
That's some rule, some guideline that we use in this recursive function.
And also we've seen that if we want to stop some at some point the recursive calls, we need to have
a in conditions some base.
Case and in this case.
We used a base cases Nahm equals to one, because we know that it's very trivial that some from one
up to one is just one.
There was no even math operations.
So this is a four of these video.
I hope that's clear how the function calls its how the function calls itself from within it.
We just call another instance.
Think of it as just on a different block units that are calling one another and expecting to receive
a result from the next one.
So thank you guys for watching and I'll see you in the next example.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.