{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineering Computation: Tutorial 06 Problem Solving with Computing, using the Python Language
Image of page 1

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

View Full Document Right Arrow Icon
Questions from Week 05? Programming Exercises from Zelle textbook 1 pg. 194 - 196 # 3, 5, 7, 10, 14. 2
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
Today’s Topics: 1 Functions 2 Scope of Variables 3 Comments 4
Image of page 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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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(). 8
Image of page 9

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

View Full Document Right Arrow Icon
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 8
Image of page 10
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
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
[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.
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern