Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 3

140 bytes added, 12:17, 14 August 2017
PART 1 - How User-Defined Functions are Declared and Run
ipython3
</source>Whenever you want to create a function, you must start with the keyword "'''def'''". The '''def''' keyword is used to start the definition of the function, it does not run the code you write. Functions, just like '''if''' statements, must have all code under them indented.<br><br>
:# Enter the following code in your ipython3 shell:<sourcelang="python">
def hello():
print('Hello World')
print('Inside a Function')
</source>Remember to press '''ENTER''' a second time to return to the ipython prompt. You may have noticed that nothing happened. Well actually, something did happen... the function called '''hello(''') has been defined and stored in internal memory in order for it to run when called by its function name. Now that our function was created, we can use it over and over. <br><br>
:#To execute the code inside the function, run the function name with "'''()'''" '''brackets''' at the end of the function name.<br>Try running the '''hello()''' function by name three times by issuing the following in the ipython3 shell:<sourcelang="python">
hello()
hello()
hello()
</source>You should notice that the function just does the same thing over-and-over no matter how many times your call the function by name. By the way, that is OK. On the other hand, you may want to create and use a function to do something, like perform error checking or some other task that returns a value to the '''main''' program for further processing. For example, a '''true''' or '''false''' value if the error checking function that was called was detected no errors or detected an error. But let's stick to some simple examples first, before tackling more complex use of functions.<br><br>
:#Let's create a function that '''returns''' some data after the function is called. This function does not print out any text: instead; it creates new variables and at the end returns the value of one of the variables.<sourcelang="python">
def return_text_value():
name = 'Terry'
return greeting
</source>
:# Call the function in your ipython3 shell by issuing the following:<sourcelang="python">
return_text_value()
</source>One major difference between a function '''returning a value''' and simply '''printing a value''' is that '''returned''' values can be caught and stored in variables used in the program (that called the function) for later use. Once the returned value has been stored, it can be printed, manipulated, compared in IF statements, etc. Below will cover how to store a returned value.<br><br>
:#Enter the following function in the ipython3 shell to see returning a variable's value works:<sourcelang="python">
text = return_text_value()
</source>
:#Now the returned text from the function has been stored in the variable "'''text'''". It can be used like any string value now.<sourcelang="python">
print(text)
</source>
:'''Perform the Following steps:'''
:#Issue the following in your ipython3 shell:<sourcelang="python">
def return_number_value():
num1 = 10
</source>
:#Now, issue the following in the ipython3 shell to call the '''return_number_value()''' function:<sourcelang="python">
number = return_number_value()
print(number)
print(return_number_value() + 10)
</source> What do you notice?<br><br>
:#Now, issue the following which use the print() statement to display both strings and numbers:<sourcelang="python">
number = return_number_value()
print('my number is ' + number)
TypeError: Can't convert 'int' object to str implicitly
</source>
:#If a number needs to be combined with a string, use the '''str()''' predefined function that was discussed in a previous lab in order to convert the returned number into a string:<sourcelang="python">
number = return_number_value()
print('my number is ', number)