The c ++ Programming Language Lecture 2 Procedure-Based Programming
The C++ Programming Language Lecture 2: Procedure-Based Programming
Procedure-Based Programming u Procedure/ Function based, why Program more readable a Code reusing a Task assignment and teamwork Prior to the oo thoughts, and still on the stage Efficient and effective in simple designs
Procedure-Based Programming ◼ Procedure/Function based, why ◼ Program more readable ◼ Code reusing ◼ Task assignment and teamwork ◼ Prior to the OO thoughts, and still on the stage ◼ Efficient and effective in simple designs
Important Concepts for Safe Programming
Important Concepts for Safe Programming
A simple starting point J Fibonacci number sequence: 11,23, 5, 8, 13, 21 a We build a function to get certain element of the sequence int fubon elem(int iPos) tn2=1,n1=1 int eLem = 1; for(intⅸ=3;ⅸ<=iPos;i++) Elem =n2+n1 n2=n1; n1= eLem return elem
A simple starting point ◼ Fibonacci number sequence: 1, 1, 2, 3, 5, 8, 13, 21, … ◼ We build a function to get certain element of the sequence int fibon_elem(int iPos) { int n2 = 1, n1 = 1; int iElem = 1; for (int iX = 3; iX <= iPos; iX++) { iElem = n2 + n1; n2 = n1; n1 = iElem; } return iElem; }
Could we always trust others' a better version J Users may not be trustworthy, assumption is dangerous Be doubtful bool fubon elem (int iPoS, int &eLem) int n2=1, n1=1 if (iPos <=0 iPos >= 1024) iElem =0: return false iElem= 1: for(intⅸ=3;ⅸ<=iPos;i++) iElem n2+n1 n1= eLem return truer
Could we always trust others? – A better version ◼ Users may not be trustworthy, assumption is dangerous ◼ Be doubtful bool fibon_elem(int iPos, int &iElem) { int n2 = 1, n1 = 1; if (iPos <= 0 || iPos >= 1024) { iElem = 0; return false; } iElem = 1; for (int iX = 3; iX <= iPos; iX++) { iElem = n2 + n1; n2 = n1; n1 = iElem; } return true; }