Hi folks. Welcome to our Module 3, the functions with return values. In first few modules, we already learned the advantages of using functions to make your program simple, to make your code can be reused, and it can be easier to share and cooperate and collaborate with your team members. Also, you can easily update your program and easily assign different parts of a big project into different teams to carry out by different members. We have already learned how to define functions with no parameter, one parameter, or multiple parameters. In those parameters will be extra inputs needed in the processing or in a group of statements in those functions to provide more flexibility and to make functions more powerful. Then in this Module, we are going to learn the value return feature for the functions. Because many of time actually we want functions not just to execute something or printing something out or do some punk computation, we wanted the functions to be able to produce something out. Still using our bread and roster example, when you insert a bread into a roster, you supposed to get a roasted bread out of the function rather than just to do the roasting without getting anything. The returning or the value returned from the function to where the function is called can be very helpful because we can continue to process that value into further computation that enables us to build up the small function, aggregating them into bigger function, and then aggregate them into the biggest or the overall in general function. We're going to learn that in more detail in Module 4. But so far, we should know that with the value-returning feature, functions can be complete and it can be quite useful. In this module, you are going to learn the concepts about a value returning function. After taking this Module, you are able to explain the need of value returning functions. You are able to define and call functions use a return value. Also we can define the core functions with multiple return values. Basically, that will be very helpful sometimes, but it will be also complex. The value which her name functions, you actually learned how to define a function with parameters. We can go over there and just to make a little bit modification. The only difference between a non-value returning or value returning is that in the group of statements with a proper intention, you need to have a return statements. The return expression will be normally the last line of the group of statements. It may not be the last line, but once the execution of the function hits or goes to this line, the function will be terminated. All the codes after returning expression will be skipped. You can use the return as a flow control as well, just to make sure that the code after return will be visited in some circumstance. For example, you have a branching, then you are fine to include a return in each of the branch. However, if you have a sequence of statements, then if you have a return or the code after the return will not be executed. That will be something you need to pay attention to. The expression basically will be about what is the value to be returned, how many value to be returned an in terms of the actual value or the variable after a certain process of computation should be returned. The value for that expression or for that variable, again, will be passed to the place where the function is called. Again, this is a pass by value iteration. The variables inside our function will only stay in the function as a local variable. It basically will disappear because once the function is terminated, the variables will be cleared and, there will be no such a variable anymore. The expression in a return statement can be a complex expression. For example, you can use an arithmetic operation. We can use x plus y, x times y, x module y, etc. You can also use another value returning function combined together to return something else, which we're going to cover in detail in next module, how we can write nested functions. Defining a function with return values is actually, as I mentioned, quite similar. You will need a definition, define function name with parameter list, and we know that already. You will need statements, and the only difference will be, the last statement will be return expression. This return expression, as we just explained, will be returning value or values to where the function is called. We're still following the same process or same syntax as we have a function header, we have the definition, we have the function name, parameter list, and then with a colon as the ending of the header, and then we have an indention for the block of codes or the list of statements. Then we emphasize that at the end we have a return expression that will be returning value. If we do not have that, then the function basically will simply execute those statements inside of the definition, and then that's all. That is how we did in previous two modules. How to use the value-returning functions. Basically it is not a secret or it is not different. The only difference is when we call the function, we are expecting the function will return something. For example, if we have a function, ask the user to enter something, then we are expecting the results or we are expecting the inputs from the user. That is why the inputs with parentheses and their instruction will return a string to us, and we want to process, and we want to do something with that string. Another example will be a very simple mathematical expression. If you take x and y, you may want to get to the summary of them or difference between them, or some statistical result of them. You may also have a complex calculation that they need to be repeated throughout the program. Then you can accumulate all the inputs together, and then return the final results. Then you can do a throughout statistic analysis for all the data you entered into your program, etc. Use the returned value, will be quite important because you design a function that will return value, and you are expecting to use it. Of course, it is your freedom to not using it at all, just execute. However, because you define your function in this way, if you just execute everything in there without a using the return value, basically, you're not using this function with the full functionality. Normally, when we have a function with a return value, once we call it, we want to assign the returned value, which is whatever executed after statements in the function and get it to a result, and then return that, we normally assign that value or values to a variable or variables. Then we can use those value or values either directly to do further computation, or we can directly use that as an argument or arguments in another function. We have already learned print, preferences, input preferences, enter whatever you like, will be a chain of functions. The input takes the user input from the input process, and the returned value as a string will be fed into the print, which is a function needed to return the string as an argument passed to the parameter, and then display that as an output on the screen. To make it clear about how a function is designed with certain input, which is the parameters, arguments, and with some process in it, and also with some possible return the value, all we can say output. Now these functions will be similar to what we learned in mathematics. It basically will be a black-box. Texts something in which is the arguments and produce something out which is the output or returned value. The IPO charts are used to represent the process of taking inputs through some process and producing some outputs. That is how we have these I, which is in input, P as a processing, and O as output. It is a tool for designing and documenting your functions so your users, your team members, and even the future of you and your clients can understand what is your design of the function and what is the functionality of these functions? It can be used in a flowchart, and it can layout it in columns 1, 2, 3. The first column will be input, second column will be processing, and the third column and the last column will be output. Also, we can provide a brief description of this IPO without going into the details because first of all, people don't care about details. Secondly, they don't have the time and you can hide your secret if you have some secret in the details. You just need to let them know what is needed for the function to process and what will be the consequence of executing this function. This will be very useful because as we know, a flowchart covers a lot of details and those details may not be needed. Actually your client may not want to or may not be allowed to know those details of your designing of your function. The IPO will be a very brief and useful description of your design of your function. We are going to show it as here for two functions. The first one is get regular price. For this one we don't need an input because for this function we basically will probe a window for the user to enter a item's regular price. Of course, you can say, if I want to include a instruction to let the user know, the user is supposed to enter a number and with how many digits after the decimal points as a float number or as integer number, or even with currency on that, etc. If you want to argue, yeah, you can put it out as some kind of input. However, that is optional. The idea of this function is to promote the user to enter something. The output basically will be whatever the user entered as the price. Then for another function, discount. Now you can use the returned value of the gate regular price from the user and then work on that value. That the value will be the input for the discount function and we are going to process a certain formula to calculate the discount based on this regular price. Then we're going to calculate and find out what is the discounted price or we have a discounted percentage. We may apply that presentation and it will get out what is the final price for this particular customer? Then our output will be the discount or the discounted price, etc. Then if you have another function, then you can just use a calculate the actual price function. You are going to use the regular price as input, as well as the discount as the input, then you can use a simple calculation and output the actual price if you want. That will be another function, and that will be some IPO you can write for that function as well. For the returned value; actually, we can return any data type of values. We can return strings which will be right functions that return for example, last name, +, and first name, and then we can concatenate the full name of a person if we have the last name and then if we have the first name, or we can simply ask the user to enter the first name or last name, and then we can produce the full name of that person, even different format. Then we can also return the yearly sale is the certain amount because that will be useful as a report if you did some calculation and you want to report a nicely conclusion of what is the summary. Then one thing you have to pay attention is, it is fine to use the plus to concatenate multiple strings to be a new string. However, if you are going to use the plus to concatenate a string with a number, either integer or float, then you may have to manually convert that number into a string, as we did here. Because Python will not do automatically converting from the float or integer to stream for you. That will be one over the difference between Python and Java or C++. You can also return a Boolean value. We learned the Boolean values has true and false. This Boolean function or return Boolean value function will be useful if we want to use it as a validator. Because sometimes we do want to check whether or not the input is valid or certain number is legitimate for us to do the process. We can write a simple function to test the legitimacy of the input and then return true or false so that you can either ask the user to do it again if you have a invalid input or you can just process if the the input is valid. We can use it to test a condition such as for decision or repetitive structures, rather than write to the condition in there and we can not use that code again because functions are for reuse of your code, and we can do that for the common calculations such as whether a number is even, can be easily repeated by calling a function rather than every time we call a if else. We can also use the Boolean function to simplify a complex input validation code. As I said, you can just simply define the validation process as a function and return the validation result to where you need that validation happen. Lastly, we can actually return multiple values, not just one. Multiple values is very powerful because this is something different, again, between Python and some other object onto the programming languages such as Java and C++, because in there they can only return one value, but in Python, you can return as many as you want, and we just interest specify after the return and in that expression, we need to separate each of the return value by commas and that's all. One thing you have to keep in mind is if you return Expression 1, Expression 2, which means you are returning two values. When you call the function, you may want to assign the two values to two different variables on the left side of the operator. That will be something you really want to pay attention to and keep in mind. We have talked through all the things you need to know about return value functions. In next report, we're going to do some in-class exercise and then revisit those functions we defined in Module 2 and turn them into return value functions. I'll see you there.