Functions and Data Passing - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Functions and Data Passing

Description:

Example Function Declarations (You've seen all of these before) ... Example (caller resumes) num1. num2. num3. result1. result2. Memory. 5. 7. 4. 6.0. 0.0. x. y ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 45
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: Functions and Data Passing


1
Functions and Data Passing
2
Overview
  • Terminology
  • Why we have functions
  • Data Passing
  • The Mantra
  • Calling Methods
  • Scope of Variables

3
Terminology
  • A function is a logical grouping of statements
  • Reusable chunks of code
  • Write once
  • Call as many times as you like
  • Benefits
  • Reusable
  • Easy to work at higher level of abstraction
  • Reduces complexity
  • Reduces size of code

4
AKA(also known as)
  • Functions can be called several things, depending
    on the book or context
  • Examples
  • Procedure
  • Module
  • Method (OOP)
  • Behavior (OOP)
  • Member function (OOP)

5
Youve Already Seen Functions
Denotes a function of the class
Denotes a property of the class
6
Invoking Functions
  • MidiPlayer.Play(new NoteOn(0, GeneralMidiPercussi
    on.BassDrum, 127))
  • Two things going on here
  • Creating a new NoteOn object
  • Passing it three parameters
  • Constructing (more later)
  • Invoking the Play method of the MidiPlayer class
  • Passing it one parameter (the NoteOn object)
  • This is a static method (more later)

7
Why have functions?(see anything similar?)
  • double average
  • int userNum1, userNum2
  • Console.WriteLine(Please enter the 2 numbers)
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • // a lot of other code
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2

8
Why have functions?(see anything similar?)
  • double average
  • int userNum1, userNum2
  • Console.WriteLine(Please enter the 2 numbers)
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • // a lot of other code
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2

All of this code is the same!
9
Basic Idea
  • double average
  • int userNum1, userNum2
  • Console.WriteLine(Please enter the 2 numbers)
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • // a lot of other code
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2

10
Basic Idea
  • double average
  • int userNum1, userNum2
  • Console.WriteLine(Please enter the 2 numbers)
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • // a lot of other code
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2

Create function instead
userNum1 Int32.Parse(Console.ReadLine()) userNu
m2 Int32.Parse(Console.ReadLine()) average
(userNum1 userNum2) / 2
11
Give the Function a Name
  • double average
  • int userNum1, userNum2
  • Console.WriteLine(Please enter the 2 numbers)
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • // a lot of other code
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2
  • userNum1 Int32.Parse(Console.ReadLine())
  • userNum2 Int32.Parse(Console.ReadLine())
  • average (userNum1 userNum2) / 2

myFunction
userNum1 Int32.Parse(Console.ReadLine()) userNu
m2 Int32.Parse(Console.ReadLine()) average
(userNum1 userNum2) / 2
12
Call the Function(instead of writing all that
code)
  • double average
  • int userNum1, userNum2
  • Console.WriteLine(Please enter the 2 numbers)
  • myFunction
  • // a lot of other code
  • myFunction
  • myFunction

myFunction
userNum1 Int32.Parse(Console.ReadLine()) userNu
m2 Int32.Parse(Console.ReadLine()) average
(userNum1 userNum2) / 2
13
What have we done?
  • Written the code once, but called it many times
  • Reduced the size of our code
  • Easier to comprehend
  • This is called procedural abstraction
  • Tracing of code skips all around (no longer
    linear)

14
Scope of Variables
  • Scope who can see what
  • Variables that are defined within a function can
    only be seen by that function!
  • We need a way to send information to the function
  • We need a way for the function to send back
    information
  • Example function1 cant see myInt

function1
function2
char myChar
int myInt
15
Example Function Declarations(Youve seen all of
these before)
  • private void buttonAdd_Click(object sender,
    EventArgs e)
  • private void FormMain_Load(object sender,
    EventArgs e)
  • private void buttonAddDigit_Click(object sender,
    EventArgs e)

16
Other Examples
  • private int sum(int x, int y)
  • private string getUserChoice()
  • private void isMatch(int guess)

17
The Mantra
  • All functions follow a mantra
  • The mantra is
  • Return type, function name, parameters
  • Return type, function name, parameters
  • Return type, function name, parameters

18
The return type
  • A function has the option to return us (the
    calling method) some information
  • If the function doesnt return us any info, the
    return type is void
  • Otherwise, the return type is the data type its
    going to return
  • Example of return types
  • int
  • char
  • boolean

19
Figure out the return type
  • Function Name
  • average double or float
  • getLetterGrade char
  • areYouAsleep bool
  • getGPA double or float
  • printMenu void
  • // Dont confuse what a function does with its
  • // return type!
  • getStudentName string

20
The functions name
  • Similar to naming of variables
  • Can be almost anything except
  • A reserved word (keywords)
  • Cant begin with a number
  • Cant contain strange symbols except _ and
  • Function names should begin with a lower case
  • Different standards/conventions are used
  • If multiple words in function name, capitalize
    the first letter in each word (except the first)
  • Example
  • thisIsAnExample

21
Parameters
  • Functions cannot see each others variables
    (scope)
  • Special variables used to catch data being
    passed
  • This is the only way the main algorithm and
    functions have to communicate!
  • Located between parentheses ( )
  • If no parameters are needed, leave the
    parentheses empty

22
Examples
  • Remember the mantra
  • What can you tell me about these functions?
  • void doSomething (int data)
  • double average (int num1, int num2)
  • boolean didHePass ( )
  • char whatWasHisGrade ( )
  • void scareStudent (char gradeOfStudent)

23
Getting the function to Work for You
  • Call it by name
  • Pass it the right stuff
  • Pass the right number of parameters
  • If it expects two things, pass it two things!
  • Pass the right type of parameters
  • If it expects a char, dont pass it a double!
  • Parameters must match exactly
  • If it returns something, do something with it!

24
Example
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
Memory
double average (int x, int y) return (
(xy) / 2)
// Note the average function is currently
inactive
25
Example (declare variables)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
0
0
0
// Note the average function is currently
inactive
26
Example (declare variables)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
0
0
0
result1
result2
0.0
0.0
// Note the average function is currently
inactive
27
Example (set values)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
0
0
result1
result2
0.0
0.0
// Note the average function is currently
inactive
28
Example (set values)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
0
result1
result2
0.0
0.0
// Note the average function is currently
inactive
29
Example (set values)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
0.0
0.0
// Note the average function is currently
inactive
30
Example (invoke function)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

WAKE UP!
x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
0.0
0.0
31
Example (data passing)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
5
7
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
0.0
0.0
32
Example (caller sleeps)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
5
7
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
0.0
0.0
// The function is now ACTIVE
33
Example (function active)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
5
7
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
0.0
0.0
// 5 7 is 12 12 / 2 is 6
34
Example (value returned)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
5
7
6
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
0.0
0.0
35
Example (caller resumes)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

sleep
x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
0.0
36
Example (call function again)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

WAKE UP!
x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
0.0
37
Example (data passing)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
4
5
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
0.0
38
Example (caller sleeps)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
4
5
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
0.0
// The function is now ACTIVE
39
Example (function active)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
4
5
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
0.0
// 4 5 is 9 9 / 2 4.5
40
Example (value returned)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

x
y
4
5
Memory
4.5
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
0.0
41
Example (caller resumes)
  • int num1, num2, num3
  • double result1, result2
  • num1 5
  • num2 7
  • num3 4
  • result1 average (num1, num2)
  • result2 average (num3, num1)

sleep
x
y
Memory
num1
num2
num3
double average (int x, int y) return (
(xy) / 2)
5
7
4
result1
result2
6.0
4.5
42
Function Rules
  • You cannot define a function inside of another
    function
  • Functions always reside in a class in C(as
    youve seen numerous time already)
  • Functions cannot see each others variables
  • Youve seen private static in our examples
    more on this later

43
An Example
  • Improving the High-Low Game
  • Add functions to clarify
  • Rule of thumb
  • Keep methods/functions small

44
Console High-Low Functions
  • static private int getUserChoice()
  • static private bool checkGuess(int guess, int
    secretNumber)
  • static private bool playAgain()
  • static void Main(string args)
Write a Comment
User Comments (0)
About PowerShow.com