Function Returning Values

5 minutes
Share the link to this page
Copied
  Completed
A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string.

Transcript

Hello, in this video, I am going to call returning values from a function. So we've got a basic function here, which squares a number times it by itself, you could have an adder that, you know, adds two numbers together. And so far, what we've got is multiply the n equals 11. So if we go over here, we run it, this is what is printing the app. But the thing is, we might want to perform some code, do some calculation, for example, in a function, but not actually printed out, we might just want to use the value we might want to store in a variable for later use. We might want to use it to update the database, we might want to, you know, Echo it out or do some formatting around echoing does, you know, a variety of reasons why you would want to manipulate a user data in a different way, the return value instead of doing this, or doing that you just put return and what's that going to do now?

So we're running, no error, but we get nothing. That's because where this is code, whatever happens in here, so what happens is num one equals two, you know itself times you know itself. So it just equals in, we can get rid of the default value, you don't need that. So who supply in 10, it will equal x in here, we need to supply to supply a number in there, and it will equal 100. But what we want to do is assign this to something. So we're going to say result equals, and now we can echo without a look, there we go, we get 100.

And if I change this to 12, that should give us 144. ones, this time by itself, and plastic and we can use this result anywhere we want. So we're not restricted to the code that's in here. So a lot of the time you will most likely use return value instead of directly using it inside of that form. Unless that function has a very narrow use case, maybe it is something like updating the database, then you would update it inside there. But you might return a for lack of status. So if it's true or false if it's you know, succeeded or not succeeded with there was an error, what was the era, so there are still use cases for not having both.

And what you could do with this, so far created function are created, whether it's a function, so this will add two numbers together, the more than two, and we're going to say, for this dollar sign equals num one plus two. And then we're going to echo we're going to return dollar. So imagine if we get two numbers from the user, and I'm going to rename this to input because you know, imagine again, from the Rename number one, which will be called input to our set this to eight. And this is going to be input one. So this chemical result one. So now, we do this twice and we so we do 12 and eight into the squared functions and we get the result back we get 144 for this wall, in this case, nettles 144.

In this case, we'll net us 60 yet 64 they write and now we want to do is add them together, and we're going to pass that into the sum function. So what we're going to do now is dollar total equals sum and then pass them both in And now we are going to echo out dollar total that we should get me 210. So tune in eight, four plus four on there is eight, and then we get the four plus six is 10 per zero carry the one. One plus one is two. So yeah, 200. Right.

And so yeah, that is a use case of returning values from functions, you can return any value. One thing to bear in mind, if you're coming from another programming language like c++, for example, in there, you are required to put the return data type, or something like that, for example. But with PHP, you know, it's pretty loose with the data types. So you can just return any value that you want, and you don't have to specifically specify what type of data type in it. And that's it any questions, feel free to pop me a message and as usual, I look forward to see you in the next video

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.