Download presentation
Presentation is loading. Please wait.
Published byBarnaby Murphy Modified over 9 years ago
1
ITEC 109 Lecture 14/15 Strings + Functions
2
Review Strings –What can we do? –Why?
3
Functions Example Find the first character in the first name and store it in a string called formatted Print out “Welcome” then print out the value of formatted then a. then a space then the last name Andrew RayA. Ray
4
Functions Backgroun d
5
Functions Methods Any that you remember? Caesar A->C, B->D, etc… Substitution –Replace A with 7 –Replace E with 3
6
Functions Sample What code would you need to write to create this encryption ?
7
Functions Objectives Functions –Parameters –Return statements –Scope –Examples
8
Functions Basics What is the purpose of functions? What are the benefits? What are the downsides?
9
Functions Visualizatio n Current view of a program New view 0 – x=3; 1 – y=x*2; 2 – printNow(y); Function call Function return
10
Functions Function Syntax def [Name] (): –def firstFunction(): Name is just like a variable name ( begins parameters ) ends parameters : begins the code Tab delimited (function begin / end implied)
11
Functions Syntax def FunctionName(): printNow(“I am a function”) def FunctionName(): printNow(“I am a function”) FunctionName() Function call Function body
12
Functions Visualizatio n def FunctionName(): printNow(“I am a function”) FunctionName() Step 1: Ignore function Step 2: Call Function Step 3: Execute Function Step 4: Return back to main Print Step 5: End program
13
Functions Return Have a function send info back 2 way street Return trip def secretValue(): return 15 myValue = secretValue() printNow(myValue)
14
Functions Parameter s Provide input to a function –Comma separated list A copy of other variables Required when function is called def add(var1,var2): return var1+var2; result = add(3,4) printNow(result)
15
Functions Scope Where variables can be used def example(): test=“Hello” printNow(test) test=“apple” example() printNow(test) Is this test the same as this test? Answer: not it isn’t
16
Functions Rules Functions are like prisons, you don’t have the freedom to go outside One way in –Parameters One way out –Return statement
17
Functions Example Writing a function that reads an int, squares, and returns it [Math.pow(var,2)]
18
Functions Example Writing a function that performs the quadratic formula
19
Functions Example Write two functions that return a separate piece of the of a string First|Last getFirst / getLast
20
Functions Philosophy Group information together Make it useful Subdivide tasks to developers
21
Functions Review Functions Parameters Return types
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.