Fundamental Programming 310201 1 Fundamental Programming Introduction to Functions.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
True or false A variable of type char can hold the value 301. ( F )
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Programming Languages
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Week 1 Algorithmization and Programming Languages.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Fundamental Programming Fundamental Programming Testing.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Fundamental Programming Fundamental Programming for Loops.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Objective: Students will be able to: Declare and use variables Input integers.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C++ Programming Lecture 12 Functions – Part IV
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Fundamental Programming Fundamental Programming Data Processing and Expressions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
310201: Fundamental Programming Fundamental Programming Introduction to Arrays.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Chapter 1. Introduction to Computers and Programming
Introduction to C++ October 2, 2017.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
Functions.
6 Chapter Functions.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Lab 1 Introduction to C++.
CS150 Introduction to Computer Science 1
Fundamental Programming
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Fundamental Programming
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Presentation transcript:

Fundamental Programming Fundamental Programming Introduction to Functions

Fundamental Programming Review Aims Of Course  develop an understanding of what programs do and how they do it  develop logical and procedural thinking  develop familiarity with the C++ language  develop skill in software development  provide an introduction to programming languages – some fundamental concepts  develop analysis and design skills  provide an introduction to structured design  encourage good s/w engineering practices

Fundamental Programming Structure Revisited  so far we’ve covered a minimal level of C++ to get you started with simple programs  also, we’ve talked about structured designs – designs broken into chunks - a main program and one or more sub-programs  in C++, sub-programs are called functions – all of our C++ programs so far have a single function – the main function; all C++ programs have a main function  even our Hello World program had a main function…

Fundamental Programming Hello World Program  in C++, the Hello World program is: #include using namespace std; void main (void) { cout << "Hello World!"; }  this program has just one function – the main function

Fundamental Programming Introduction to Functions  functions will be the focus of our work over the next two weeks, and in Assignment 2  our strategy is to get these ideas out early and use tutes and labs to build up your comfort level  so, if the topics covered this week are a bit slippery – DON’T PANIC!

Fundamental Programming Structured Programs  perhaps the place to begin our examination of functions is to use a very simple example  lets take a look at a version of the Hello World program that uses two functions…

Fundamental Programming Structured Hello World Program #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; }

Fundamental Programming Hello World Program  in this design, the main function simply calls the DisplayHelloWorld function void main(void) { DisplayHelloWorld(); }  the DisplayHelloWorld function sends a message to the display and returns to the calling function – the main function  like this…

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } program starts in main function

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } => program starts in main function

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } =>

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } main function calls DisplayHelloWorld function =>

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } control passes to function DisplayHelloWorld =>

Fundamental Programming #include void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } =>

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } DisplayHelloWorld function sends Hello World! message to display =>

Fundamental Programming Example dialog Hello World!

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } no more to do in DisplayHelloWorld - return to main function =>

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } control returns to main function… =>

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } no more to do in main function - program ends =>

Fundamental Programming Declarations  we saw previously that, in C++, we had to declare a variable before we use it  the same is true for functions…

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } DisplayHelloWorld function is declared here DisplayHelloWorld function is called here DisplayHelloWorld function is defined here

Fundamental Programming #include using namespace std; void DisplayHelloWorld(void); void main(void) { DisplayHelloWorld(); } void DisplayHelloWorld(void) { cout << "Hello World!"; } notice semicolon at end of this line – this is the end of the declaration notice no semicolon at end of this line – this is the start of the definition Jargon : we call the declaration of a function a function prototype

Fundamental Programming What do we mean by void ?  we’re ready to start talking about void … void DisplayHelloWorld(void)  the second void indicates that the function has no input variables or output variables  lets’ look at a function with some input and output variables…  the structured design used previously has a sub-program with one input variable and one output variable…

Fundamental Programming Pseudocode Example main write “Number of marks in exam ==> “ read NbrMarks get valid student mark set Percentage to 100 * StudentMark / NbrMarks write “ Student’s percentage: “ write Percentage if Percentage < 50 then write “ (Fail)” else write “ (Pass)” sub-program get valid student mark

Fundamental Programming Pseudocode Example get valid student mark input: NbrMarks set StudentMark to -1 while StudentMark NbrMarks write “Student’s mark ==> “ read StudentMark if StudentMark NbrMarks then write “ERROR: Enter a value between 0 and ” write NbrMarks write NewLine output: StudentMark sub-program uses the value of input variable NbrMarks to perform it’s task sub-program assigns a value to output variable StudentMark – it is used later in the program

Fundamental Programming Input-Process-Output  sub-programs can have one or more inputs and can produce one or more outputs - recall the input-process-output model  example: get valid student mark input process output NbrMarks StudentMark

Fundamental Programming Structure Chart main get valid student mark NbrMarks StudentMark

Fundamental Programming Function Inputs and Outputs  in C++, the get valid student mark sub- program looks like this…

Fundamental Programming void GetValidStudentMark(int NbrMarks, float &StudentMark) { StudentMark = -1; while ((StudentMark NbrMarks)) { cout "; cin >> StudentMark; if ((StudentMark NbrMarks)) { cout << "ERROR: Enter a value between 0 and "; cout << NbrMarks; cout << endl; } notes: * no spaces in function name

Fundamental Programming void GetValidStudentMark(int NbrMarks, float &StudentMark) { StudentMark = -1; while ((StudentMark NbrMarks)) { cout "; cin >> StudentMark; if ((StudentMark NbrMarks)) { cout << "ERROR: Enter a value between 0 and "; cout << NbrMarks; cout << endl; } notes: * no spaces in function name * input and output variables instead of void

Fundamental Programming void GetValidStudentMark(int NbrMarks, float &StudentMark) { StudentMark = -1; while ((StudentMark NbrMarks)) { cout "; cin >> StudentMark; if ((StudentMark NbrMarks)) { cout << "ERROR: Enter a value between 0 and "; cout << NbrMarks; cout << endl; } notes: * no spaces in function name * input and output variables instead of void * data types declared in function header

Fundamental Programming void GetValidStudentMark(int NbrMarks, float &StudentMark) { StudentMark = -1; while ((StudentMark NbrMarks)) { cout "; cin >> StudentMark; if ((StudentMark NbrMarks)) { cout << "ERROR: Enter a value between 0 and "; cout << NbrMarks; cout << endl; } notes: * no spaces in function name * input and output variables instead of void * data types declared in function header * output variable name prefixed by & Jargon : input and output variables are called parameters

Fundamental Programming Function Inputs and Outputs  in C++, the rest of the program looks like this…

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: * declaration – header plus ”;”

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: * declaration – as defined plus ; * no data types in function call

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main(void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: * declaration – as defined plus ; * no data types in function call * order of variables in call must match order in declaration

Fundamental Programming program declarations main() function implementation code #include directives function declarations (prototypes)    function definitions (implementation code)    Function1 Function2 Function1 Function2 Structure of a C++ Program

Fundamental Programming Naming Functions  same as variable names…  start with a letter - A to Z  can contain upper or lower case letters, or numerals - 0 to 9  good idea to limit names to 32 characters  must avoid C++ reserved words like int, float, void, etc  more later…

Fundamental Programming void parameters  we now know that a void in parentheses after the function name means that the function has no input of output variables  what about the first void ?  we’ll talk about it later… void GetValidStudentMark(int NbrMarks, float &StudentMark); void DisplayHelloWorld(void)

Fundamental Programming Activity  a program is needed to encrypt personal data  a prime number is needed for the encryption algorithm  to get started, we will write a program to get a prime number from the user  ask the user to enter a prime number  if number entered is not prime, ask user if they wish to use the nearest prime number  make a start on this program now…

Fundamental Programming Example dialog Enter a prime for encryption key ==> is nearest prime – use it? [Y/N] ==> n Enter a prime for encryption key ==> is nearest prime – use it? [Y/N] ==> n Enter a prime for encryption key ==> 23

Fundamental Programming Activity  use the following functions in your design: GetNearestPrime - input: Number (integer) output: NearestPrime (integer)  note: nearest prime to 7 is 7; if the number entered is equal to it’s nearest prime – it’s a prime number  here is a starting point for this program, finish it…

Fundamental Programming #include void GetNearestPrime(int Number, int &NearestPrime); void main(void) { char Done = ‘N’; int Number = 0; :  other variables? while (Done == ‘N’) { cout "; cin >> Number; : } Activity

Fundamental Programming Activity Break

Fundamental Programming #include void GetNearestPrime(int Number, int &NearestPrime); void main(void) { char Done = ‘N’; int Number = 0, Prime = 0; while (Done == ‘N’) { cout "; cin >> Number; GetNearestPrime(Number, Prime); if (Number = = Prime) Done = ‘Y’; else { cout << Prime; cout “; cin >> Done; } note: name of variable in function call can differ from name in function Activity Sample Solution

Fundamental Programming Functions and Variables  let’s learn more about the relationship between variables and functions …

Fundamental Programming Functions and Variables  GetValidStudentMark uses only 2 variables – NbrMarks and StudentMark (the parameters) void GetValidStudentMark(int NbrMarks, float &StudentMark) { StudentMark = -1; while ((StudentMark NbrMarks)) { cout "; cin >> StudentMark; if ((StudentMark NbrMarks)) { cout << "ERROR: Enter a value between 0 and "; cout << NbrMarks; cout << endl; }

Fundamental Programming Functions and Variables  however, most functions will use addition variables to perform their task  another design for GetValidStudentMark is…

Fundamental Programming void GetValidStudentMark(int NbrMarks, float &StudentMark) { int InputMark = -1; while ((InputMark NbrMarks)) { cout "; cin >> InputMark ; if ((InputMark NbrMarks)) { cout << "ERROR: Enter a value between 0 and "; cout << NbrMarks; cout << endl; } StudentMark = InputMark; } Jargon : InputMark is called a local variable – it is local to the function; it cannot be used in main, or any other function

Fundamental Programming Local Variables  most functions use local variables  later we will learn about global variables – and why we avoid using them  as the name suggests, local variables are only available (or visible) locally - they cannot be used from outside the function  unauthorised use of a function’s local variables will result in a compilation error…

Fundamental Programming #include void SubFunction(void); void main(void) { int LocalToMain; SubFunction(); LocalToSub = 1; } void SubFunction(void) { int LocalToSub; LocalToMain = 1; } local variables declared in main and SubFunction compilation errors here…

Fundamental Programming Local Variables  unless authorised, a function’s local variables cannot be used by other functions  global variables do not provide this protection – they are visible everywhere  use of global variables is discouraged as they can make programs more difficult to debug…

Fundamental Programming Local vs Global Variables  programmers debug programs by tracing the value of variables as a program runs…  a variable assigned a value at the start of a function – a function that does not authorise sub-functions to use it - may be assumed to still hold that value at the end of the function  if it’s a global variable, this may not be the case – a sub-function may have changed it

Fundamental Programming void SomeFunction(void) { : GlobalVariable = InitialValue; : < functions called here, but no authority given to change GlobalVariable > : OutputValue = ; : } programmer may assume GlobalVariable still holds initial value here… but, it’s value may have been changed by one of the functions called here

Fundamental Programming void SomeFunction(void) { : GlobalVariable = InitialValue; : < functions called here, but no authority given to change GlobalVariable > : OutputValue = ; : } functions do not need authority to change global variables - dangerous! - can make programs harder to debug - do not use global variables after assignment 2

Fundamental Programming Local vs Global Variables  we will learn how global variables are declared later…  for now, lets’ explore this idea of authorising other functions to use local variables  how do you do it?  why would you want to?  how does it work?

Fundamental Programming Exposing Local Variables  the first two questions are easy to answer  how do you do it?  why would you want to?  we’ve already seen an example of how you do it…

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: StudentMark is a local variable

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: function GetValidStudentMark is called to obtain a valid student mark

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: this mark is assigned to variable StudentMark – this is an output value produced by GetValidStudentMark

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } notes: so, main’s local variable called StudentMark is assigned a value by function GetValidStudentMark

Fundamental Programming #include void GetValidStudentMark(int NbrMarks, float &StudentMark); void main (void) { int NbrMarks = 0; float StudentMark = 0, Percentage = 0; cout "; cin >> NbrMarks; GetValidStudentMark(NbrMarks,StudentMark); Percentage = 100 * StudentMark / NbrMarks; cout << " Student's percentage: "; cout << Percentage; if (Percentage < 50) { cout << " (Fail)"; } else { cout << " (Pass)"; } conclusion: authority to change a local variable is granted whenever we call a function with an output variable

Fundamental Programming Exposing Local Variables  so, answers to our first two questions are:  how do you do it?  answer: call a sub-function with an output variable  why would you want to?  answer: to receive output values produced by sub-functions

Fundamental Programming Memory Management  an answer to our third question takes a bit more explaining - how does it work?  to answer this question, we need to explain how output variables work  later, we will explore the meaning of void in: void GetNearestPrime(int Number, int &NearestPrime); void GetYorNReply(char &Reply);

Fundamental Programming Functions - The Jargon #include void SubFunction(int LocalVariable); void main(void) { int LocalVariable = 1; SubFunction(LocalVariable); cout << "LocalVariable after call is: "; cout << LocalVariable; } void SubFunction(int LocalVariable) { LocalVariable = 2; } function declaration or prototype function parameter function call function header Prototype is a copy of Header with a semicolon