COMP 103 L6-Functions - Stud.
COMP 103 L6-Functions - Stud.
Programming
FUNCTIONS
Charles Fomevor
CHECKLIST
1.HOW MANY PROGRAMS HAVE YOU WRITTEN
TO DATE?
2.HOW MANY PROGRAMS HAVE YOU COMPILED
TO DATE?
3.HOW MANY PROGRAMS HAVE YOU
DEBUGGED TO DATE?
4.HOW MANY PROGRAMS HAVE YOU GOT
WORKING CORRECTLY TO DATE?
5.GET HELP BUT DO IT YOURSELF MORE
Functions
• Although object-oriented programming has
shifted attention from functions and toward
objects, functions nonetheless remain a central
component of any program.
• We look at the following:
– What a function is and what its parts are.
– How to declare and define functions.
– How to pass parameters into functions.
– How to return a value from a function.
What Is a Function?
• A function is, in effect, a subprogram that can
act on data and return a value.
• Every C++ program has at least one function,
main(). When your program starts, main() is
called automatically.
• main() might call other functions, some of
which might call still others.
What Is a Function? (continued)
Each function has its own name, and when that
name is encountered, the execution of the
program branches to the body of that function.
When the function returns, execution resumes on
the next line of the calling function.
●
14: cin >> widthOfYard;
●
15: cout << "\nHow long is your yard? ";
●
16: cin >> lengthOfYard;
●
17:
●
18: areaOfYard= FindArea(lengthOfYard,widthOfYard);
●
19:
●
20: cout << "\nYour yard is ";
●
21: cout << areaOfYard;
●
22: cout << " square feet\n\n";
●
23: return 0;
●
24: }
●
25:
●
26: USHORT FindArea(USHORT l, USHORT w)
●
27: {
●
28: return l * w; yard? "; 29: }
●
Defining the Function
• The definition of a function consists of the
function header and its body.
• The header is exactly like the function
prototype, except that the parameters must be
named, and there is no terminating semicolon.
• The body of the function is a set of statements
enclosed in braces.
• Next slide shows the header and body of a
function.
Header and body of function
• Function Prototype Syntax
return_type function_name ( [type
[parameterName]]...);
●
13: cout << TempCel << endl;
●
14: }
●
15:
●
16: float Convert(float Fer)
●
17: {
●
18: float Cel;
●
19: Cel = ((Fer - 32) * 5) / 9;
●
20: return Cel;
●
21: }
SCOPE
• A variable has scope, which determines how
long it is available to your program and where
it can be accessed.
• Variables declared within a block are scoped to
that block; they can be accessed only within
that block and "go out of existence" when that
block ends.
SCOPE
●
●
14: int n, answer;
●
15: cout << "Enter number to find: ";
●
16: cin >> n;
●
17:
●
18: cout << "\n\n";
●
19:
●
20: answer = fib(n);
●
21:
●
22: cout << answer << " is the " << n << "th Fibonacci number\n";
●
23: return 0;
●
24: }
●
25:
●
26: int fib (int n)
●
27: {
●
28: cout << "Processing fib(" << n << ")... ";
●
29:
●
30: if (n < 3 )
●
31: {
●
32: cout << "Return 1!\n";
●
33: return (1);
●
34: }
●
35: else
●
36: {
●
37: cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
●
38:
●
4: // Uses this algorithm: Fib(n) = fib(n-1) + fib(n-2)
●
5: // Stop conditions: n = 2 || n = 1
●
6:
●
7: #include <iostream.h>
●
8:
●
9: int fib(int n);
●
10:
●
11: int main()
●
12: {
●
13:
●
14: int n, answer;
●
15: cout << "Enter number to find: ";
●
16: cin >> n;
●
17:
●
1
CHECKLIST
• HOW MANY PROGRAMS HAVE YOU WRITTEN
TO DATE?
• HOW MANY PROGRAMS HAVE YOU COMPILED
TO DATE?
• HOW MANY PROGRAMS HAVE YOU DEBUGGED
TO DATE?
• HOW MANY PROGRAMS HAVE YOU GOT
WORKING CORRECTLY TO DATE?
• GET HELP BUT DO IT YOURSELF MORE
Next Week
●
…
●