Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University 2010-2011.

Similar presentations


Presentation on theme: "COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University 2010-2011."— Presentation transcript:

1 COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University 2010-2011

2 Functions Function A named sequence of statements that performs some useful operation Functions may or may not take arguments and may or may not produce a result, named as return value. It is common to say that a function “takes” an argument and “returns” a result. Function Calls A statement that executes a function. It consists of the name of the function followed by a list of arguments enclosed in parentheses. You have already seen one example of a function call: >>> type("32") the name of the function is type it has as argument with value “32” it produces a result as the type of value and displays the result

3 Functions(cont.) Examples >>> id(3) 134882108 >>> betty = 3 >>> id(betty) 134882108 Every value has an id, which is a unique number related to where it is stored in the memory of the computer

4 Functions(cont.) Examples (cont.) Type conversion functions: int, float, str The int function takes any value and converts it to an integer, if possible, or complains otherwise: >>> int("32") 32 >>> int("Hello") ValueError: invalid literal for int(): Hello int can also convert floating-point values to integers, but remember that it truncates the fractional part: >>> int(3.99999) 3 >>> int(-2.3) -2

5 Functions(cont.) Math Functions sin, cos, log, pi etc. Python has a math module A module is a file that contains a collection of related functions grouped together Before we can use the functions from a module, we have to import them >>> import math

6 Functions(cont.) Math Functions(cont.) To call one of the functions, we have to specify the name of the module and the name of the function, separated by a dot, also known as a period. This format is called dot notation. >>> decibel = math.log10 (17.0) >>> angle = 1.5 >>> height = math.sin(angle)

7 Functions(cont.) New Function specifying its name, parameters, and the statements. Use any name (cannot use keywords) include any number of statements inside The statements do not get executed until the function is called Syntax def NAME( LIST OF PARAMETERS ): STATEMENTS Example def newLine(): print () This function is named newLine. The empty parentheses indicate that it has no parameters. It contains only a single statement, which outputs a newline character.

8 Functions(cont.) New Function(cont.) Syntax for Calling New Function print "First Line." newLine() print "Second Line." Output: First line. Second line.

9 Functions(cont.) New Function(cont.) Exercise: Write a function that prints three lines, and name it threeLines. You can call the same procedure Repeatedly or You can have one function call another function; in this case threeLines calls newLine.

10 Functions(cont.) Parameters and Arguments arguments, the values that control how the function does its job. if you want to find the sine of a number, you have to indicate what the number is as an argument Inside the function, the values that are passed get assigned to variables called parameters. Here is an example of a user-defined function that has a parameter: def printTwice(bruce): print (bruce, bruce) Variables and parameters are local When you create a local variable inside a function, it only exists inside the function, and you cannot use it outside

11 Exercise Write a function called printParity. Example use is as following: printParity(17) Output: 17 is odd y = 17 printParity(y+1) Output: 18 is even

12 Exercise Solution

13 Return Values Some functions produce results, or return values import math def area(radius): temp = math.pi * radius**2 return temp This statement means: “Return immediately from this function and use the following expression as a return value.”

14 Exercise Write a compare function that returns 1 if x > y, 0 if x == y, and -1 if x < y.

15 Exercise As an example, suppose you want to find the distance between two points, given by the coordinates (x1, y1) and (x2, y2). By the Pythagorean theorem, the distance is:

16 Exercise Solution


Download ppt "COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University 2010-2011."

Similar presentations


Ads by Google