t_06[1] - Engineering Computation: Tutorial 06 Problem...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 Whats 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 Todays Topics: 1 Functions 2 Scope of Variables 3 Comments 4 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. 5 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 (theres 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? Lets define another function, called Hello2(). 8 Hello() with parameters What if we want our program to say Hello to someone other than the World? Lets define another function, called Hello2(). >>> def Hello2(name): ... print "Hello", name We can now invoke the function like this: >>> Hello2("William") Hello William 8 Hello() with parameters What if we want our program to say Hello to someone other than the World? Lets 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....
View Full Document

Page1 / 58

t_06[1] - Engineering Computation: Tutorial 06 Problem...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online