Week 7 - Friday

cornawakeΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

85 εμφανίσεις

Week 7
-

Friday


What did we talk about last time?


Software engineering


Testing


Programming languages

Language

Description

Typing

C/C++

Fast and powerful systems language, prone to nasty bugs

Static

Java

Platform

independent OO language, uses virtual machine

Static

Objective
-
C

Apple language

for writing Mac, iPhone, and
iPad

apps

Static

C#

Microsoft language

for applications and the web, like Java

Static

Python

Interpreted language with unusual syntax,
easy to read

Dynamic

Perl

Scripting language, good at processing text

Dynamic

Ruby

Interpreted

language, popular for making websites

Dynamic

PHP

Interpreted, C
-
like language that runs on web servers

Dynamic

JavaScript

Interpreted language that runs on web browsers

Dynamic

Visual Basic

Microsoft language, good a
t making GUIs

Static

SQL

Language for talking to databases

Static


We can classify languages on a spectrum
from low to high


High level languages allow you to give more
abstract commands that are more like human
thought processes or mathematics


Low level languages are closer to the
computer world and give explicit instructions
for the hardware to follow

ML

Java

C++

C

Assembly
Language

Machine
Code

Low

High


We use a program called a
compiler

to turn a
high level language into a low level language


Usually, the low level language is machine
code


Python uses an
interpreter

instead of a
compiler


An interpreter runs the program line by line
instead of turning it into a machine code file
that the OS can run

Computer!

Solve a
problem;

010101010

010100101

001110010

Execute



Source
Code

Machine
Code

Hardware


Functions

allow you to package up some
code to run over and over


Functions usually take some input (like
numbers or
text
) so that they can be
customized


Functions
often

give back an answer (like the
square root of a number)


Also called
methods

in some other languages


The customizable purple blocks played the
role of functions in Scratch




More modular programming


Break a program into separate tasks


Each task could be assigned to a different
programmer


Code reusability


Use code over and over


Even from other programs


Less code duplication


Improved readability


Each function can do a few, clear tasks


def

name( arg1, … ,
argn

):




statement1


statement2





statementm


Function name

Name of

last argument

Name of

1
st

argument

Required syntax

Code done by function


Given two numbers, find the smaller:

def

smaller
(a, b):


if
a < b:



return
a


else

:



return
b


It is possible to divide functions into two types:


Functions that return values


Functions that don't return values


Functions that do return values give an answer:





It's as if the function is replaced by whatever
answer is returned



x = 3

y = 4

small

=

smaller(x, y)
#small contains 3


A function doesn't have to return an answer:






This function only prints things to the screen


A function that doesn't return anything does
some task but doesn't give back an answer

def

callFor
H
elp
(times):



for

i

in

range
(times):



print
(
"
Help
!"
)



Like most code in Python, the code inside of a
function executes line by line


Of course, you are allowed to put
if

statements and loops inside methods


You can also put in
return

statements


A function will stop executing and jump back
to wherever it was called from when it hits a
return


The
return

statement is where you put the
value that will be given back to the caller


Defining a function is only half the game


You have to
call

functions to use them


Calling a function means giving it the
parameters

(or
arguments
) it needs and then
waiting for its answer


By now, you have done many function calls


input()


print()



You can call your own functions the same way



You type the name of the function and then its
arguments in parentheses





If it's a value returning function, you can store the
answer it gives back





The arguments you give can be values or variables


You have to give the function the right number of
arguments or the program will have an error

callForHelp
(3)
#calls for help 3 times

result

=

smaller(9, 2)


Every function starts with
def


Then comes the name of the function


Followed by the names of the arguments in
parentheses


Followed by a colon (
:
)


Like loops and if statements, the code inside
a function is indented one level


Call
the function in
other code by typing the
name and the correct number of arguments
in parentheses


Lets say we want to make a function that will
draw three balls stacked on top of each other


The bottom one is green, the middle is orange,
and the top is red


We want the function to take a radius and a
position


The bottom ball will be drawn at the position
with the radius given


The middle ball will be above it with half the
radius


The top ball will be above that with one quarter
the radius


Here's the function
stacks()

which takes a
radius and a position and draws the balls

def

stacks
(radius, position):

sphere(radius=radius
,
pos
=position, color=
color.green
)

newPosition
=(
position.x
,
position.y

+
3*radius/2,position.z)

sphere(radius=radius/2
,
pos
=
newPosition
,
color=
color.orange
)

newPosition
=
(
position.x
,
position.y

+ 9*radius/4,
position.z
)

sphere(radius=radius/4
,
pos
=
newPosition
,
color=
color.red
)


Now we can call the
function several times


Each time, it will draw a
stack with the size and
location we specify






Result shown to the right

stacks(1, vector(0,0,0))

stacks(.5, vector(1,2,3))

stacks(.2, vector(
-
2,1,2))


Moore's law


Multicore computers


Complex decisions


Lab 7


Finish Project 2


Due tonight before midnight!


Reading Python Chapter 7


Think about what you want to do for your
Final Project


Proposal due by 10/18