24
[CSE10200] Programming Basis (프로그래밍 기초) Chapter 4-2 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

[CSE10200] Programming Basis

(프로그래밍 기초)

Chapter 4-2

Seungkyu Lee

Assistant Professor, Dept. of Computer Engineering

Kyung Hee University

Page 2: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Functions in C++

Page 3: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

User-Defined Functions

- Declaring, Calling, and Defining

Page 4: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Functions without Return Value

(Only Have Side Effect)

Page 5: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Functions with Return Value

Page 6: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Function Return Statements

Page 7: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Pass by Value

vs

Pass by Reference

Page 8: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Pass by Value

Different local

variables

Page 9: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Pass by Reference

Page 10: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

A Bad Exchange

Page 11: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Calculate Quotient and Remainder

Page 12: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Program 4-8

Page 13: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Default Parameter Arguments

• Default values for parameters can be defined in function declaration

Program 4-9

Page 14: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Other Functions

Page 15: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Library Functions and the Linker

Page 16: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Standard Library Functions

Page 17: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Floor and Ceiling Functions

Page 18: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

abs(), pow(), sqrt() and rand()

• abs(3) returns 3

• fabs(-3.4) returns 3.4

• pow(3.0, 4.0) returns 81 (3^4)

• pow(3.4, 2.3) returns 16.687893

• sqrt(25.0) returns 5.0

• rand()? srand()?

Page 19: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Scope for Global and Block Areas

Variables are in

scope from their

point of definition

until the end of

their function or

block.

Page 20: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Program 4-12

Page 21: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Program 4-13

Page 22: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Tips and Common Errors

Page 23: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Tips and Common Errors

Page 24: Seungkyu Lee - khu.ac.krcvlab.khu.ac.kr/Lecture6.pdf · 2016-03-30 · 12 Program 4 This function adds two integers re turns sum. Pre Post Parameters a b Returns a + b add (int a,

Tips and Common Errors