22
LECTURE 16: FUNCTION BASICS & SCOPE CSC 107 – Programming For Science

CSC 107 – Programming For Science. Today’s Goal Discuss writing & using functions How to declare them, use them, & trace them Could write programs

Embed Size (px)

Citation preview

Page 1: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

LECTURE 16:FUNCTION BASICS & SCOPE

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Today’s Goal

Discuss writing & using functions How to declare them, use them, & trace

them Could write programs using functions

Page 3: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Today’s Goal

Discuss writing & using functions How to declare them, use them, & trace

them Could write programs using functions

Already been doing this, but should clarify process

Page 4: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Functions

Already written programs using functions One function always present in all

programs: main cos, sin, floor also functions you've called

before main is a programmer-defined function

Pre-defined functions are similar, but part of C++

Programmers can define and use own functions

cosradians cosine

Page 5: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Functions

Already written programs using functions One function always present in all

programs: main cos, sin, floor also functions you've called

before main is a programmer-defined function

Pre-defined functions are similar, but part of C++

Programmers can define and use own functions

FunctionParameters Return Value

Page 6: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Why We Use Functions

Simplify code Replace copies of code by placing in single

location Commonly-used math function

computation Each function can return a single value

Input & output performed in these functions Will discuss ways to change parameters’

values

Page 7: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Function Basics

Functions must be defined to use Otherwise will get an “Undefined Symbol”

error Declared before call so has a chance to

check if legal Since all functions equal, define outside of main

No specific order within file to define functions Book recommends main be placed first in

file Requires declarations for each function main uses

But this will simplify figuring out what program does

Page 8: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Return Type

Each function must declare a return type Type of value returned by a functionfloat abs(float x);double pow(double x, double t);int main();

May want nothing returned : use void as return typevoid printFormattedNumber(int x);void readAndPrintAverage();

Must return value from non-void function If function is void, cannot return value

Page 9: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Function Declaration

When definition is after calling function… Could be that function is later in file Function in another file for use in many

programs Also important for built-in functions without

bodies Declare functions at start of the file

Often listed in header (*.h) files to enable reuse

#include "copy-and-paste" code from those files

Declaration lists vital information: function signature

Page 10: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Function Declarations

Declarations list return type, name, & parameters What is necessary for legal call to function

shown Allows compiler to flag errors when they exist

double pow(double x, double y);int isLetter(char c);float cos(float rad);

Page 11: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Function Declarations

Declarations list return type, name, & parameters What is necessary for legal call to function

shown Allows compiler to flag errors when they exist

double pow(double x, double y);int isLetter(char c);float cos(float rad);float squareNumber(float x);double computeAvg(int x, int y);int main();

Page 12: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Function Definitions

Eventually every function must be defined Where code written specifying actions for

functions Each time we call function, these will be

executed Already defined 1 function: each program's main

Function definition outline is always samereturnType name(paramType paramName) {

statement;statement;…statement;

}

Page 13: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

Variables create name for memory address Name is not universal, but limited by the

scope Variable usable only in braces in which

declared For this copy of variable, scope defines its

lifetime Variable "dies" with end of scope in which

declared At start of scope, new copy created Cannot use outside scope: error for bad

variable Must have unique names within a

scope If no overlap, can reuse names since old

name gone

Page 14: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

int num = 3;

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {readNumber(num);readNumber(5);return 0;

}

Page 15: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

int num = 3;

void readNumber(int len) {int num = 0; // This is name is not unique here!for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {readNumber(num);readNumber(5);return 0;

}

Variable Scope

Page 16: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {int num = 3;readNumber(num);readNumber(5);return 0;

}

Page 17: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {int num = 3;readNumber(num);readNumber(5);return 0;

}

Page 18: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {int num = 3;readNumber(num);readNumber(5);return 0;

}

Page 19: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {int num = 3;readNumber(num);readNumber(5);return 0;

}

Page 20: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Variable Scope

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

int main() {int num = 3;readNumber(num);readNumber(5);return 0;

}

Page 21: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

Let's Trace

void readNumber(int);

int main() {int num = 3;readNumber(num);readNumber(5);return 0;

}

void readNumber(int len) {int num = 0;for (int i = 0; i < len; i++) { char ch; cin >> ch; num *= 10; num += ch – '0';}cout << num << endl;

}

Page 22: CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs

For Next Lecture

Read more about functions in Sections 9.3-9.4.3 Describe how value returned from a

function What happens to code after the return

statement? How do we pass values to a function? What can a function do to those values?

Weekly Assignment #6 out & due next Wed. Avoid the rush by start working on it now Note special due date, since no school on

Tuesday