Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Programming 310201 1 Fundamental Programming Introduction to Functions.

Similar presentations


Presentation on theme: "Fundamental Programming 310201 1 Fundamental Programming Introduction to Functions."— Presentation transcript:

1 Fundamental Programming 310201 1 Fundamental Programming Introduction to Functions

2 Fundamental Programming 310201 2 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

3 Fundamental Programming 310201 3 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…

4 Fundamental Programming 310201 4 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

5 Fundamental Programming 310201 5 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!

6 Fundamental Programming 310201 6 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…

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

8 Fundamental Programming 310201 8 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…

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

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

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

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

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

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

15 Fundamental Programming 310201 15 #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 =>

16 Fundamental Programming 310201 16 Example dialog Hello World!

17 Fundamental Programming 310201 17 #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 =>

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

19 Fundamental Programming 310201 19 #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 =>

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

21 Fundamental Programming 310201 21 #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

22 Fundamental Programming 310201 22 #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

23 Fundamental Programming 310201 23 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…

24 Fundamental Programming 310201 24 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

25 Fundamental Programming 310201 25 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

26 Fundamental Programming 310201 26 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

27 Fundamental Programming 310201 27 Structure Chart main get valid student mark NbrMarks StudentMark

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

29 Fundamental Programming 310201 29 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

30 Fundamental Programming 310201 30 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

31 Fundamental Programming 310201 31 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

32 Fundamental Programming 310201 32 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

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

34 Fundamental Programming 310201 34 #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 ”;”

35 Fundamental Programming 310201 35 #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

36 Fundamental Programming 310201 36 #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

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

38 Fundamental Programming 310201 38 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…

39 Fundamental Programming 310201 39 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)

40 Fundamental Programming 310201 40 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…

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

42 Fundamental Programming 310201 42 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…

43 Fundamental Programming 310201 43 #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

44 Fundamental Programming 310201 44 Activity Break

45 Fundamental Programming 310201 45 #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

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

47 Fundamental Programming 310201 47 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; }

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

49 Fundamental Programming 310201 49 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

50 Fundamental Programming 310201 50 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…

51 Fundamental Programming 310201 51 #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…

52 Fundamental Programming 310201 52 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…

53 Fundamental Programming 310201 53 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

54 Fundamental Programming 310201 54 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

55 Fundamental Programming 310201 55 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

56 Fundamental Programming 310201 56 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?

57 Fundamental Programming 310201 57 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…

58 Fundamental Programming 310201 58 #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

59 Fundamental Programming 310201 59 #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

60 Fundamental Programming 310201 60 #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

61 Fundamental Programming 310201 61 #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

62 Fundamental Programming 310201 62 #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

63 Fundamental Programming 310201 63 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

64 Fundamental Programming 310201 64 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);

65 Fundamental Programming 310201 65 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


Download ppt "Fundamental Programming 310201 1 Fundamental Programming Introduction to Functions."

Similar presentations


Ads by Google