Engineering 1020 Introduction to Programming Peter King peter

  • Slides: 9
Download presentation
Engineering 1020 Introduction to Programming Peter King peter. king@mun. ca www. engr. mun. ca/~peter

Engineering 1020 Introduction to Programming Peter King peter. king@mun. ca www. engr. mun. ca/~peter Winter 2010

ENGI 1020: Updates • – – • • • – – Either book is

ENGI 1020: Updates • – – • • • – – Either book is fine for this course: C++ Without Fear, Brian Overland, Prentice-Hall, ISBN 0321 -24695 -0. Problem Solving Abstraction and Design Using C++, Frank Friedman and Elliot Koffman, Addison Wesley. (used copies ok) I have an office for office hours EN-3017 Thursdays 11 -12 Today is last day I will sign people in Labs start this Tuesday

ENGI 1020: Functions • Functions are the smallest of modules • Remember all that

ENGI 1020: Functions • Functions are the smallest of modules • Remember all that hierarchy we talked about? • All active code lives in a function • We’ve met main() • Functions generally wrap up a specific task, duty or operation • Write once, use often

ENGI 1020: Functions • Functions must be declared before use, before main() • Declaration

ENGI 1020: Functions • Functions must be declared before use, before main() • Declaration includes: • Return type • int, double, char, float, bool, etc. • If none we use void • Function name • Remember the rules! • It should be somewhat descriptive • max. Value, circle. Area, avg. Speed • Arguments • What data is sent to the function to produce an output • Type and identifier • (), (int x), (float speed, float distance) • Semi colon

ENGI 1020: Functions • Functions are defined outside of main, but after they are

ENGI 1020: Functions • Functions are defined outside of main, but after they are declared • Definition includes • Function declaration (prototype) • As before, but with no ‘; ’ • Open curly brace { • This is a code block • The statements that make up the function • return statement at end if data is to be returned • Close curly brace }

ENGI 1020: Functions • Functions are called from within other functions • Often in

ENGI 1020: Functions • Functions are called from within other functions • Often in main() • Function calls must look like definition and satisfy the contract • a function basically defines an agreement that if provided with a certain set of input arguments, it will provide a certain output • A variable must be there to accept the output • The arguments must match the prototype

ENGI 1020: Functions • Example: • A function to print “hello world” to the

ENGI 1020: Functions • Example: • A function to print “hello world” to the screen • void print. HW() Definition void print. HW(); Declaration void print. HW(){ cout << “hello world” << endl; } Call print. HW();

ENGI 1020: Functions • Online examples * • Comments and Contracts * • Good-Bad

ENGI 1020: Functions • Online examples * • Comments and Contracts * • Good-Bad examples * • Sample problems *

ENGI 1020: Functions Code examples

ENGI 1020: Functions Code examples