#12 - Functions in TypeScript || Anonymous Function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is Navia welcome back to na automation labs and back to our typescript Series today we are going to talk about functions in typescript so we all know that what do you mean by function functions are a kind of block of code where we have to write some logic some business logic a function can be void also function can WR return anything and a function can take parameters also like this right so we will see different varieties of functions so first of all that how to declare a function in typescript is just like the way we declared the functions in JavaScript now here we can give the type of the parameter also and we can Define that function is void or not that also we can do it here so for example a very basic function that we are going to talk about that named function named function means a function which is having us which is having some name for example let's see if I'm declaring a function here and giving any name here for example give me the info let's see get info function that I have WR and I'm printing something here that in double code let's see I'm printing a hello info here like this so this is a simple function that I have created here so this is a function name you can write the function name whatever the function name you want to write it is start with the function here and then having these two parenthesis that I'm passing it so in these two parenthesis I'm not passing any parameter so can I say this is zero parameter function because I'm not passing anything and I'm just printing hello info here so how to call this function in order to call this function you just simply call it by the function name that you have return and I'm not passing anything because this is zero parameter so when I call this function it will print hello info on the console so let's quickly do that I'm just going to compile this and then I'm going to run it once again with the node so let's run it with function. JS file and then you can see Hello info is getting printed on the console so this is a very simple function that we have created now let's do one thing let's create one parameterized function where I'm passing something so for example let's see if I'm passing one number here or let's see I'm adding there is one function the function name is ADD and I really want to add two numbers so what we have to do here I'm passing that okay give me the the value of a and then a I'm expecting a number here and give me the value of B and B the type of B is also a number here so how many parameters we are passing two parameters here and then I'm printing one thing here that whatever A and B that you are passing and I'm just writing a plus b here and print it on the console and when I call this particular function see I'm calling this function now and here I'm passing 10 comma 20 here right so add function will be called 10 will be given to a 20 will be given to B and it will give you the result as 30 here so remember when I call this function this is called call by value right I'm passing two values here 10 and 20 and these 10 and 20 is are also called arguments here so when I call the function and when I pass these two values these are called arguments and when I say this particular function when I declare it and I'm taking these two parameters these are not called arguments so when I call it it is called argument and parameters when I Define it like this these are called parameters here so remember people might ask you at a time of interview that what is the difference between parameter and the argument so when I run this program 10 and 20 will be printing will be given and then this function will print just 30 here so let's quickly run it you have to compile it again and then I'm going to run it again and you can see see 30 is coming here simple can we have other parameters also yes n number of parameters that whatever you want to add you can add it here there is no issue with that simple okay now what if I really want to return something from the function can we do that yes we can that also let's create another function so let's see here I'm writing a function that let's see uh calculate some numbers here like this right right and I'm passing couple of parameters here let's see first parameter here I'm writing in the form of number and the second parameter also here I'm writing that in the form of number and then let's see one more parameter I'm passing in the form of a number here like that okay so three parameters I'm passing here and I'm creating one variable here let's see one let variable and the let variable say that total is equal to whatever A + B plus or let's C minus C that is what I have have return and then I really want to return total from this particular function so what exactly you are returning you are returning a total so total if you really want to give the type of the total you can give it the total I'm expecting it should be a number type and I'm returning a total so what will be the return type of this function the return type of this function will be total and total is a number so here in typescript you can Define the return type of the function also in Java in C what we do we write something like this integer or double or something like this but here in JavaScript you have to write after the function name after the parameter simple colon number here now this function is more readable if you see that see there is no mandatory it's not compulsory that you have to write number here you can avoid that by default the type annotations will be entered or it will in infair the the data type of the function or return type of the function it will automatically do that but it's always a good practice in the typ script that tell what exactly you're going to return here so now if I call calculate numbers and I'm passing let's see three parameters here 30 comma 40 comma 50 here so what will be the output so 30 will be given to ABC I mean 30 40 50 will be given to ABC respectively it will calculate a plus b - c so 30 + 40 70 - 50 is equal to 20 so total will be 20 will be written back here so now I have to store it in some variable because when I call this function this function is giving me something it's taking three input parameters or arguments and then giving me something here so give me something let's see I'm storing it in some result variable so let's see result variable here and this result variable also will be a number so I'm defining the type of the result also here and I'm printing it on the console so let's print it result here okay so let's compile it again and then run it again so now you can say yes we are getting 20 here right is it a good practice to return something from the function yes it is always a good practice instead of printing directly on the console better you return something from the function and then what if tomorrow I really want to make some changes whatever the result is coming let's see I really want to add Plus 90 here or some extra calculation that I really want to do or really want to supply this result to some other function I can do it here so it's always a good practice that whenever you're doing some calculation or you want to return something always return something from the function like this okay can we return a Boolean from the function of course we can do that so see this another function that I'm going to create so let's create one more function here that function that is I really want to check that user is active or not right and I'm saying that you have to give me a user name so let's see I'm writing a username will be a a string type of the username so this is is user active and I want that this function should give me a Boolean here okay it mean I want that the function should give me a Boolean and I'm writing one simple condition here that if whatever the user name that you are supplying this username dot or equal to equal to equal to is equal to let's see some username let's see that is naine here then I want that okay fine the user is active return true from here like this but another else condition let's see adding one more username here so if username is equal to equal to something else let's see uh Tom here then Tom is not active then I'm writing return false from this particular function but it still is giving me the error why we are returning true and false but it's still giving me Boolean it says that what if the condition is not satisfied he says function liks ending return return statement and return type does not include undefined means what if the function I mean condition is not satisfied then I'll say okay fine I'll come inside the else part and then also I'm saying return false from here it means I'll pass something that okay user is not found or something like this let's see I'm returning it like that right so that's what if the user name is not found let's say I'm passing Peter Peter is not available in that case I'll print a message first that console do log here and then I'm printing that this username is not found like this which username the username that you are supplying it to me simple so in that case what will happen let's simple call this particular function so I'm calling this is user active and then I'm passing ing naven here for example and I really want to check that this user is really active or not so naven will be given to this username condition is satisfied it will return true so it is giving me true here I can store it in another Boolean type of variable or can I use it directly inside my if condition that if user active like this if user is active which user naine then I'll say that console. log and let's see login login with naven here something like this or whatever you want to write it here or some other further action on the basis of return you want to do you can do it here right so in that case let's quickly run it again so again I'm first of all compiling it and then I'm running it here with the node so you can say yeah login with naven is coming here because when I pass naven here the condition will be satisfied right if I'm passing let's see Peter here Peter is not not there so it will print username is not found so condition will be false so don't need to log in in that case so again run it again so let's compile it and then run it so here you say that okay yeah username is not found this user Peter is not available here right you can put a space Also after found so you can return anything whatever you want to return from a function remember whenever I have to create a function if I don't want to return anything see I'm not returning anything from this particular function so this function is called a void type of function can I declare a void type yes if you really want to write you can write colon and void also you can write it so there is no harm you can write it like this also so whenever a function is declared with the return type void it mean this function cannot return anything the moment see if I try to write okay return 30 here void and return cannot be together remember this thing right but if I remove this return 30 and only write only return then it is fine so this is called a blank return or you can say a void return also you can say that so when I'm not returning anything fine we can write it like this also but remember the moment I try to return something something mean let's see anything let's say I want to return a plus b no I cannot do that void and return cannot be together okay so just we avoid writing blank void or void written if you really want to add it you can add it here like this as well simple okay so this is what a normal function that we have created now the second type of function the first function that we have seen right named function another function we will create without name function without name means Anonymous function we can also do that so if you have seen my JavaScript series we have covered Anonymous a function also a function what do you mean by Anonymous function a function without name this is called Anonymous function right what exactly we do we create a without name without name function we created and store it in a variable that's it simple formula create a function without name but to store it in a variable and then call it using the variable name only okay let's see how to do that so for example let's see I'm creating a function see without name I'm just directly writing here see the difference earlier we were creating function and function name we are writing call numbers add function get info function name here I'm directly writing these two parenthesis here and then I'm saying okay this function is doing something something means let's say sprinting uh hello typescript so this function I have created now I have to store this function in a some variable so let's create a variable here whatever the variable name let's see I'm writing print variable so let print is equal to like this so I think print is already a reserved blocks code print was already declared here so let's write something else let's see here I'm writing naven here whatever the name you want to give you can give it here okay so let's see here I'm writing info here info variable that I have written so how to call this function call it using the variable name only so see I'm going to call it info n as a variable name I'm calling it so what will be the output info method means info function will be called it will print hello TS here okay so let's run it once again compile it and then run it so here you can say hello TS is coming here like that so these functions are called Anonymous function which does not have any name okay now can we pass the parameters also in the anonymous function yes you can pass the parameter also so let's create one more function here let's see I'm writing let and some function is equal to I wanted okay give me the addition or sum of two numbers again I'm writing function without name so I'll immediately put a bracket here like this and I'm passing X as number here and then I'm passing y also as a number here and I want that okay from this function please return the number only so this is a return type okay so what will you return I simply say return whatever the X and Y add these two numbers and return so how will you see this particular function this function has no name but is stored in some variable variable name is sum and this function takes two parameters X and Y both are numbers and the return type of this function is number only and this is the business logic that I have have return now I want to call this particular function so by using the variable name that we have stored so Su and I'm passing two variables here let's see 5 comma 6 so 5 comma 6 I'm passing it will return what five will be given to X Y will be six so 11 will be given to this guy and then I can store it in some variable here so let's see this is my another Rees variable or result variable whatever you want to write it will be number which is equal to this so and then I'm printing it on the console that whatever the result Rees value will be there just printed on the console so let's run it again so compile it and then with the node run it again you can say 11 is coming on the console perfect so this is I can do it here like this right so in the next session we will talk about other types of functions also like we will talk about uh Arrow functions and function overloading and the rest parameters and all those things also we will see so remember this thing that yes these are different types of various type of functions that we can create it you can create Arrow function also that we will see in the next chapter and in the next chapter we will talk about that what do you mean by optional parameters and the default parameters also I think that's all for this particular video thank you so much please practice typescript and let me know in case of any issues
Info
Channel: Naveen AutomationLabs
Views: 1,651
Rating: undefined out of 5
Keywords: Functions in TypeScript, Anonymous Function, arrow functions, js functions, javascript tutorials, typescript tutorials, typescript for react, typescript by naveen automation labs
Id: G2ybfBX2oxI
Channel Id: undefined
Length: 17min 26sec (1046 seconds)
Published: Wed Nov 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.