CSE 1341 Principles of Computer Science I - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

CSE 1341 Principles of Computer Science I

Description:

Connect to UNIX server using Exceed (in Lab) or PuTTY (outside of lab) ... PuTTY. 45. UNIX and vi. Tutorials available at: http://www.ee.surrey.ac.uk/Teaching/Unix ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 47
Provided by: admi1144
Category:

less

Transcript and Presenter's Notes

Title: CSE 1341 Principles of Computer Science I


1
CSE 1341Principles of Computer Science I
Note Set 2
2
Semester Overview
  • Functions
  • Character File I/O
  • Arrays
  • Pointers and Dynamic Memory Allocation
  • Structures/Records
  • Binary File I/O

3
Today
  • Divide a program into functions
  • Pass data to functions by value
  • Differentiate between local and global variables
  • Define variables with static storage
    classifications

4
Review of Functions
5
  • FUNCTION
  • a collection of statements that performs a
    specific task
  • Why modularize?
  • To divide programs into small, manageable tasks
  • To simplify programs
  • To reduce code size
  • Simplify modification and maintenance processes

6
4 Parts of a Function
  • double square(int x)
  • int result x x
  • return result

7
Calling a Function
  • void displayMessage()
  • cout ltlt Hello from function
  • ltlt dislplayMessage.\n
  • int main()
  • cout ltlt Hello from main\n
  • displayMessage() //Function Call
  • cout ltlt Back in function main\n
  • return 0

8
Function Header vs Function Call
void displayMessage() ______________ displayMessag
e() _____________
9
Function Header vs Function Call
  • void displayMessage()
  • cout ltlt Hello from function
  • ltlt dislplayMessage.\n
  • int main()
  • cout ltlt Hello from main\n
  • displayMessage()
  • cout ltlt Back in function main
  • ltlt again.\n
  • return 0

10
Multiple Functions
  • int main()
  • coutltltStart main\n
  • first()
  • second()
  • coutltltFini main\n
  • return 0
  • void first()
  • cout ltlt In first\n
  • void second()
  • cout ltlt In Second\n

Output???
11
Functions with Parameters
12
Parameters
  • Parameter
  • special variables that hold a value being passed
    into a function
  • Argument
  • Values sent to a function

int result pow (2, 4) void displayVal(int num)
13
Functions with Parameters
  • void displayValue(int num)
  • coutltltThe value is
  • ltlt num ltlt endl
  • int main()
  • coutltltPassing Values\n
  • displayValue(5)
  • displayValue(10)
  • return 0

Output???
14
Multiple Parameters
Function Name
Return Type
Parameter List
  • void calc(int num1, int num2, int num3)
  • int temp num1 3
  • cout ltlt ( tempnum2(num32) ) ltlt endl

15
Multiple Parameters
  • void calc(int num1, int num2, int num3)
  • int temp num1 3
  • cout ltlt ( tempnum2(num32) ) ltlt endl
  • int main()
  • int value1 3, value2 4, value3 5
  • calc(value1, value2, value3)
  • return 0

How would output change with the function
call calc(value2, value1, value3)
16
Exercise
  • Write a function that prints the largest of two
    integers that are passed in as arguments.
  • Write three calls to this function passing
    different data
  • what are some things to think about testing?
    Special Cases?

17
Function Prototypes
18
Function Prototypes
  • Function definition or function prototype must
    appear before any function call in the source
    code.
  • The compiler must know the correct, legal
    format of a function call.
  • It can get this info from
  • Function Header
  • Function Prototype

19
Prototype vs. Function Header
  • Prototype
  • Must have return data type
  • Must have name
  • Must be terminated with semi-colon
  • Must have parameter data types
  • May have parameter names
  • Function Header
  • Must have return data type
  • Must have name
  • Must have parameter data types
  • MUST have parameter names
  • Not terminated with semi-colon

Note Both must list all parameters. Prototype
must agree with the function header
20
Prototype vs. Function Header
  • void showSum(int, int, int)
  • int main()
  • int value1 3, value2 4, value3 5
  • showSum(value1, value2, value3)
  • return 0
  • void showSum(int num1, int num2, int num3)
  • cout ltlt (num1num2num3) ltlt endl

Prototype
Function Header
21
Passing by Value
22
Passing by Value
  • When an argument is passed to a function, only a
    copy of the actual argument is passed.
  • Changes to a function parameter do not affect the
    original argument sent to the function.

23
Passing by Value
  • void modify(int)
  • int main()
  • int var 3
  • cout ltlt var ltlt endl
  • modify(var)
  • cout ltlt var ltlt endl
  • return 0
  • void modify(int num1)
  • num1 10
  • cout ltlt num1 ltlt endl

24
Pass by Value Recap.
  • Passing by value NEVER changes the value of the
    original argument passed to the function.

25
Local vs. Global Variables
26
Local vs. Global Variables
  • Local Variables
  • Known only to the function in which it is
    declared
  • Cannot be accessed outside the function
  • Global Variables
  • Known to all functions in the same file
  • Can be known to other functions in other files
  • Cannot be accessed inside a function if that
    function has a variable by the same name unless
    the scope resolution operator is used

27
Local Variables
  • void anotherFunc()
  • int main()
  • int num 1
  • cout ltlt Num is ltlt num ltlt endl
  • anotherFunc()
  • cout ltlt Now num is ltlt num ltlt endl
  • return 0
  • void anotherFunc()
  • int num 20
  • cout ltlt In anotherFunc, num is
  • ltlt num ltlt endl

1
num
20
num
28
Local Variable Example
  • void func()
  • int main()
  • int num 11
  • coutltltIn main, num is ltlt num ltlt endl
  • func()
  • coutltltBack in main, num is ltlt num ltlt endl
  • void func()
  • int a 20
  • cout ltlt In func, num is ltlt num ltlt endl

Whats wrong??
29
Global Variables
  • void anotherFunction()
  • coutltltIn func, num
  • ltltnumltltendl
  • num 50
  • coutltltbut now, num
  • ltltnumltltendl
  • void anotherFunction()
  • int num 2
  • int main()
  • coutltltmain, num
  • ltlt num ltlt endl
  • anotherFunction()
  • coutltltback in main
  • ltltnum ltltnum
  • ltltendl
  • return 0

2
num
30
Global Variables
  • void anotherFunction()
  • coutltltIn func, num
  • ltltnumltltendl
  • num 50
  • coutltltbut now, num
  • ltltnumltltendl
  • void anotherFunction()
  • int num 2
  • int main()
  • coutltltmain, num
  • ltlt num ltlt endl
  • anotherFunction()
  • coutltltback in main
  • ltltnum ltltnum
  • ltltendl
  • return 0

50
num
2
31
Global Variables
  • void anotherFunction()
  • coutltltIn func, num
  • ltltnumltltendl
  • num 50
  • coutltltbut now, num
  • ltltnumltltendl
  • void anotherFunction()
  • int num 2
  • int main()
  • coutltltmain, num
  • ltlt num ltlt endl
  • anotherFunction()
  • coutltltback in main
  • ltltnum ltltnum
  • ltltendl
  • return 0

50
num
2
32
Global Variables Whats Wrong?
  • void anotherFunction()
  • coutltltIn func, num
  • ltltnumltltendl
  • num 50
  • coutltltbut now, num
  • ltltnumltltendl
  • include ltiostreamgt
  • using namespace std
  • void anotherFunction()
  • int main()
  • coutltltmain, num
  • ltlt num ltlt endl
  • anotherFunction()
  • coutltltback in main
  • ltltnum ltltnum
  • ltltendl
  • return 0
  • int num 2

Access before definition
Point num is only accessible AFTER it has been
declared
33
  • Problem!!
  • What if you have a global variable and local
    variable that have the same name? How do you
    access one of the other?

34
Global AND Local
  • int val 1
  • int main()
  • int val 111
  • cout ltlt Local val ltlt val
  • ltlt endl
  • cout ltlt Global val ltlt val
  • ltlt endl
  • return 0

Use scope resolution operator to access global
variable
35
Storage Classifications
36
Storage Classification of Variables
  • auto default type, automatic variables are the
    ones weve been using all along
  • extern tells the computer that the variable is
    defined elsewhere in the program
  • register tells the computer to use one of the
    CPUs registers
  • static value persists (its value remains) even
    after leaving the function

37
Non-Static Local Variable
  • void showLocal()
  • int main()
  • for(int i 0 i lt 4 i )
  • showLocal()
  • return 0
  • void showLocal()
  • int localNum 5
  • cout ltlt localNum ltlt localNum
  • ltltendl
  • localNum 99

5 5 5 5
Will always display 5
38
Non-Static Local Variable
  • void showStatic()
  • int main()
  • for(int i 0 i lt 4 i )
  • showStatic()
  • return 0
  • void showStatic()
  • static int localNum 5
  • cout ltlt localNum ltlt localNum
  • ltltendl
  • localNum

5 6 7 8
Defined only once
39
Other Stuff
40
Blackboard
  • Make Sure you can log in to BB
  • Explore

41
Using UNIX and vi
  • Connect to UNIX server using Exceed (in Lab) or
    PuTTY (outside of lab)
  • UNIX vi tutorials and quizzes
  • Read, comprehend, and work through the tutorials
    that are linked on outline and on Blackboard.
  • Need to be completed by the end of Week 3 Lab
  • Take the quizzes when you feel comfortable, but
    before end of lab for 3rd week. Quiz link will
    go away.
  • Quizzes are password protected have TA type in
    PW for you

42
Using UNIX
  • Many UNIX Servers to choose from
  • apoc
  • tank
  • janus
  • Access using Exceed from in Lab
  • Access using (from outside SOE Lab)
  • PuTTY (ssh mode for off campus)

43
PuTTY
  • Excellent way to login to UNIX
  • To Download
  • Google for putty
  • Choose second link PuTTY Download Page

44
PuTTY
45
UNIX and vi
  • Tutorials available at
  • http//www.ee.surrey.ac.uk/Teaching/Unix
  • http//heather.cs.ucdavis.edu/matloff/UnixAndC/Ed
    itors/ViIntro.html

46
Questions?
?
Write a Comment
User Comments (0)
About PowerShow.com