Trying to get a grasp on functions prior to continuing my attack on the assignment (see my last entry for that temporary hot mess.) Reading my textbook chapter on functions. My notes below...
What is a Function?
A subprogram that can act on data and return a value.
*main() is a function. When your program starts, main() is automatically called and run. There can be multiple functions within main().
*global functions -- functions not part of an object.
*each function has its own name. When that name shows up in the code, the function executes.
***well designed functions perform a single, specific, and easily understood task, identified by the function name.
*built-in functions: part of compiler package
*user-defined functions: defined / written by user (duh)
===================
Return Value: result of work done by a program or function
int MyFunction(); //will return an integer value
int myFunction(int someValue, float someFloat); //will take two values and return an int.
Parameter List: description of values you send into a function (the value/number types, int and variable name, or float and variable name (may be other options but these are all I know so far))
Arguments: values (numbers, etc) passed through a function
int theValueReturned = myFunction (5, 6.7);
myFunction is the function, values 5 and 6.7 are the arguments, and the type of arguments must match the declared parameter type -- (int someValue, float SomeFloat);
====================================
prototype: declaration of a function (calling the function to run within the program)
3 ways to declare a function:
-1 write your prototype into a file and use #include to include it in your program
-2 write the prototype into the file in which your function is used.
-3 define the function before it is called by any other function. The function acts as its own prototype using this method. (But don't do this because it requires functions to be in a particular order which ruins most of the point of using functions in the first place.)
Function prototype: a statement that consists of the function's return type and signature (the name and parameter list for the function)
- if you don't want your function to return a value you can write void in front of it (I don't understand why you'd want to do that, but i find out I'll let you know)
Function definition: function header and body. the header looks like the function prototype except the parameters must be named and no semicolon at the end is used. The body is the set of statements enclosed in braces.
=========================
According to my professor, the proper way to write a function is:
1) write function call
(call for the function you haven't written yet)
2) draw box with parameters and return type
3) write prototype AND comment
4) write the full function definition
========================================
Ok... now I'm going to try to tackle my assignment again now that I understand functions a little more.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment