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