Program name: user-defined-function.c
Language features of C contained in "user-defined-function.c"
function: is a subdivision of a larger program
has the role of a "program with a program"
has a name
typically it "gets and gives"
accepts data from the larger program it belongs to
does something with it to produce new, derivative data
returns the derived result to the larger program
Function: accepts an integer from the user, figures out its opposite (using an internal function to figure), prints the opposite
Source code: Here is a C program that contains a subdivision called a function, by the name of "opposite". Typically functions are bits of code within a program that take in some data and, after processing it in some way, feed back some data which is the function's result.
Explanations:
This program has a second function in it. In addition to the required, central main( ) function you see one called opposite( ). The code in main( ) can use opposite( ) whenever it might wish to. Here, it does so in line 15.
Typically a function is handed some input, performs some kind of processing upon it, and hands back the result. Handed by who? Handed back to what? Well, whatever code is using the function. Here, opposite( ) is handed a number in/by line 15. The role of opposite is to produce the opposite of what it's given. Given 6, produce -6; given -111, produce 111.
opposite( ), like main( ), has the same structure. there are a pair of curly braces that hold the its code, the code that goes under the name "opposite" (lines 5 and 6). Notice that the "give back" by opposite is implicit. That is, the "opposite( number )" call to opposite( ) is embedded in the printf( ) function call.
Notice also line 5. If this were algebra it would be a false equation. Any number and its opposite are different, not equal (well, with the exception of zero). But this is an instruction for a series of actions taken. Whatever value is currently in the variable "input" is retrieved, on the right side of line 5. Then it's negated. Then the negated result is place back into the "input" variable. That's the result of the assignment, by the equals sign in line 6. Remember, you should think of a variable as a container for a value. The procedure represented in line 5 is "take it out, change it, put it back." The changed result then is what gets "return"ed by line 6, to line 15.
To do:
Key this program in, using a text editor. Save it in a file named "user-defined-function.c". Then, in order to convert it into form that contains correct Intel machine instructions, thus enabling the computer to run it, bring up the Windows command box and give this command:
bcc32 user-defined-function.c
Alternatively use a browser to go to a C compiler web site. Cut your program's text from the text editor and paste it into the appropriate place in the web site. Tell the web site to compile (there's generally a button that says "compile" or maybe "run".)
If there are no error messages, a new file named "user-defined-function.exe" will result. Then give the command:
user-defined-function.exe
A question appears on the monitor, "What is your favorite integer?" and the program waits for you to type one in. After you type it, and press the enter key, it produces the opposite of your integer and reports it to you. If give it 5, for example, it reports -5. If you give it -99, it reports 99.