Not only can you pass a parameter value into a function, a
function can also produce a value. You have already seen this in some previous functions that you have used. For example, Functions that return values are sometimes called fruitful functions. In many other languages, a function that
doesn’t return a value is called a procedure, but we will stick here with the Python way of also calling it a function, or if we want to stress it, a non-fruitful function. How do we write our own fruitful function? Let’s start by creating a very simple mathematical function that we will call The return statement is followed by an expression which is evaluated. Its result is returned to the caller as the “fruit” of calling this function. Because the return statement can contain any Python expression we could have avoided creating
the temporary variable Notice something important here. The name of the variable we pass as an argument — Activity: CodeLens 12.5.3 (clens11_4_1) There
is one more aspect of function return values that should be noted. All Python functions return the special value Activity: CodeLens 12.5.4 (clens11_4_2) The problem with this function is that even though it prints the value of the squared input, that value will not be returned to the place where the call was done. Instead, the value A return statement, once executed, immediately terminates execution of a function, even if it is not the last statement in the function. In the following code, when line 3 executes, the value 5 is returned and assigned to the variable x, then printed. Lines 4 and 5 never execute. Run the following code and try making some modifications of it to make sure you understand why “there” and 10 never print out. The fact that a return statement immediately ends execution of the code block inside a function is important to understand for writing complex programs, and it can also be very useful. The following example is a situation where you can use this to your advantage – and understanding this will help you understand other people’s code better, and be able to walk through code more confidently. Consider a situation where you want to write a function to find out, from a class attendance list,
whether anyone’s first name is longer than five letters, called In this case, you’ll be using conditional statements in the code that exists in the function body, the code block indented underneath the function definition statement (just like the code that starts with the line Bonus challenge for studying: After you look at the explanation below, stop looking at the code – just the description of the function above it, and try to write the code yourself! Then test it on different lists and make sure that it works. But read the explanation first, so you can be sure you have a solid grasp on these function mechanics. First, an English plan for this new function to define called
Now, the code: So far, we have just seen return values being assigned to variables. For example, we had the line Function invocations, however, can also be used as part of more complicated expressions. For example, To reiterate, when executing a line of code
Check your understanding What is wrong with the following function definition: def addEm(x, y, z): return x+y+z print('the answer is', x+y+z) What will the following function return? def addEm(x, y, z): print(x+y+z) What will the following code output? def square(x): y = x * x return y print(square(5) + square(5)) What will the following code output? def square(x): y = x * x return y print(square(square(2))) What will the following code output? def cyu2(s1, s2): x = len(s1) y = len(s2) return x-y z = cyu2("Yes", "no") if z > 0: print("First one was longer") else: print("Second one was at least as long") Which will print out first, square, g, or a number? def square(x): print("square") return x*x def g(y): print("g") return y + 3 print(square(g(2))) How many lines will the following code print? def show_me_numbers(list_of_ints): print(10) print("Next we'll accumulate the sum") accum = 0 for num in list_of_ints: accum = accum + num return accum print("All done with accumulation!") show_me_numbers([4,2,3]) 8. Write a function named 9. Write a function called 10. Write a function called 11. Write a function called 12. Write a function named 13. Write a function called 14. Write a function called You have attempted of activities on this page What is a module that returns a value back to the part of the program that called it?A value-returning function is a function that returns a value back to the part of the program that called it.
Which function returns a string that is within another string?Returns a Variant (Long) specifying the position of the first occurrence of one string within another.
What is a return value quizlet?it is the value to be returned. it can be any expression that has a value such as a variable, literal, or mathmatical expression. Write the definition of a function oneLess, which receives an integer parameter and returns an integer that is one less than the value of the parameter.
What are the pieces of data that are passed into a module called?is any piece of data that is passed into a module when the module is called. parameter; is a variable that receives an argument that is passed into a module. When two parameter variables are declared inside the parentheses in the module header.
|