DOC PREVIEW
TAMU CSCE 110 - Functions
Type Lecture Note
Pages 3

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 7Outline of Last Lecture:I. LogarithmsA. What are they?B. How are they useful to us?II. FunctionsOutline of Current Lecture:I. FunctionsA. ArgumentsB. Pass/ReturnC. MainII. Using functions to write a programCurrent Lecture:I. FunctionsA. Let's look at a function that, when called, tells the user whether a number is even or odd.>>> def iseven(num):>>> print num % 2 == 0The "num" in the parentheses denotes that the function "iseven" is expecting an argument when called. To clarify, "num" is a variable local to only the function "iseven" - meaning that it will not have any meaning if used in the rest of the program. This allows us to enter the number we want to establish as even or odd into the parentheses. Here are some examples of input/output:>>> iseven(10)>>> True>>> iseven(7)>>> FalseWe are able to have as many arguments as we would like, as long as we separate them with a ", " within the parentheses. Here is an example of input/output:>>> def print_msg(x, y):>>> print xThese notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.>>> print y>>> print_msg(1010, 'string')>>> 1010>>> stringNotice that we had to identify the second argument as a string by placing it in single quotations.If we had left it without quotation marks, we would receive an error when running the program.If a function has no argument in its definition and we call it with one, it will also give back an error.B. There are two other key words we will go over, "pass" and "return." Pass merely makes a function's definition obsolete. For example, if we have a program...>>> def print_msg(x, y):>>> pass...there will be neither an error nor any output when we call the function.Return simply gets an output value ready for further use. Here is an example of a program that uses return:>>> def is_even(num):>>> return num % 2 == 0Now we can call the function within a command. Here is an example of how we call the functionand its input/output:>>> print is_even(4)>>> TrueC. Say we have multiple functions we'd like to call. We can make a separate function called "main" that includes multiple other functions. This way, outside definitions of functions, we onlyhave to have one line of command. For example:>>> def printing(x):>>> print x>>> def main():>>> printing(12)>>> printing("string")And now, to call it, all we have to input to receive our output is:>>> main()>>> 12>>> stringII. Using functions:Let's write a program that converts a given temperature and temperature type to either Celsius or Fahrenheit. We will need several types of functions and variables:1 >>> def fahrenheit_to_celsius(fahrenheit):2 >>> celsius = (fahrenheit - 32) * 5.0/93 >>> print 'Celsius: ', celsius4 >>> def celsius_to_fahrenheit(celsius):5 >>> fahrenheit = celsius * (9.0/5) + 326 >>> print 'Fahrenheit: ', fahrenheit7 >>> def main():8 >>> temperature = int(raw_input('Enter a temperature: '))9 >>> temperature_type = raw_input('Convert to Celsius(c) or Fahrenheit(f)? ')10 >>> if temperature_type == 'c':11 >>> celsius_to_fahrenheit(temperature)12 >>> else:13 >>> fahrenheit_to_celsius(temperature)14 >>> main()If the user inputs a temperature in Fahrenheit, the order of line execution will be:14, 7, 8, 9, 10, 12, 13, 1, 2, 3If the user inputs a temperature in Celsius, the order of line execution will be:14, 7, 8, 9, 10, 11, 4, 5, 6Notice that both programs start with line 14 because that is the first line of code that is not a function


View Full Document

TAMU CSCE 110 - Functions

Type: Lecture Note
Pages: 3
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?