{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

t_06.4up

t_06.4up - Questions from Week 05 Engineering Computation...

This preview shows pages 1–4. Sign up to view the full content.

Engineering Computation: Tutorial 06 Problem Solving with Computing, using the Python Language Questions from Week 05? Programming Exercises from Zelle textbook 1 pg. 194 - 196 # 3, 5, 7, 10, 14. 2 What’s in your toolbox? Print statement, string literals Basic calculator, integer division Variables Built-in functions Math library functions Non-branching programs Branching programs Python lists For loops Nested for loops Strings Converting between number system using Python Matplotlib Algorithms File Processing Graphics 3 Today’s Topics: 1 Functions 2 Scope of Variables 3 Comments 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Functions Definition Building blocks of larger, more complex programs. A small program within a program. Can be re-used over and over again without the need to re-write code. Form of a Function Definition def functionName(parameters): block 5 A simple example Instead of typing... >>> print "Hello, World!" ...every time we need to run that code (we are very lazy programmers), we can define a function called Hello() like this: >>> def Hello(): ... print "Hello, World!" ... >>> This is called the function definition . Now, to call (a.k.a. invoke) our function we just type Hello(): >>> Hello() Hello, World! 6 Function Parameters The brackets after the function name is where we list any parameters (values or variables) we want to pass to the function. Remember, the function is a small program onto itself. It cannot read a variable outside itself. It cannot write to a variable outside itself. The function Hello() does not take any parameters (there’s nothing in the brackets). >>> def Hello(): ... print "Hello, World!" ... >>> 7 Hello() with parameters What if we want our program to say Hello to someone other than the World? Let’s define another function, called Hello2(). >>> def Hello2(name): ... print "Hello", name We can now invoke the function like this: >>> Hello2("William") Hello William Easy, right! 8
More aobut Parameters Parameters act as inputs into a function. Parameters provide a way to initialize a variable in a function. A function can be called many times, giving different results if the inputs provided are different. 9 [DEMO] Write a function that sums two numbers that are passed into the function as parameters. Call the function sumTwo . The function should print the result. 10 Another Example: We want to have a function that will sum the numbers in a list.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

t_06.4up - Questions from Week 05 Engineering Computation...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online