
- •Table of Contents
- •Introduction
- •About This Here Dummies Approach
- •How to Work the Examples in This Book
- •Foolish Assumptions
- •Icons Used in This Book
- •Final Thots
- •The C Development Cycle
- •From Text File to Program
- •The source code (text file)
- •The compiler and the linker
- •Running the final result
- •Save It! Compile and Link It! Run It!
- •Reediting your source code file
- •Dealing with the Heartbreak of Errors
- •The autopsy
- •Repairing the malodorous program
- •Now try this error!
- •The Big Picture
- •Other C Language Components
- •Pop Quiz!
- •The Helpful RULES Program
- •The importance of being \n
- •Breaking up lines\ is easy to do
- •The reward
- •More on printf()
- •Printing funky text
- •Escape from printf()!
- •A bit of justification
- •Putting scanf together
- •The miracle of scanf()
- •Experimentation time!
- •Adding Comments
- •A big, hairy program with comments
- •Why are comments necessary?
- •Bizarr-o comments
- •C++ comments
- •Using Comments to Disable
- •The More I Want, the More I gets()
- •Another completely rude program example
- •And now, the bad news about gets()
- •The Virtues of puts()
- •Another silly command-prompt program
- •puts() and gets() in action
- •More insults
- •puts() can print variables
- •The Ever-Changing Variable
- •Strings change
- •Running the KITTY
- •Hello, integer
- •Using an integer variable in the Methuselah program
- •Assigning values to numeric variables
- •Entering numeric values from the keyboard
- •The atoi() function
- •So how old is this Methuselah guy, anyway?
- •Basic mathematical symbols
- •How much longer do you have to live to break the Methuselah record?
- •The direct result
- •Variable names verboten and not
- •Presetting variable values
- •The old random-sampler variable program
- •Maybe you want to chance two pints?
- •Multiple declarations
- •Constants and Variables
- •Dreaming up and defining constants
- •The handy shortcut
- •The #define directive
- •Real, live constant variables
- •Numbers in C
- •Why use integers? Why not just make every number floating-point?
- •Integer types (short, long, wide, fat, and so on)
- •How to Make a Number Float
- •The E notation stuff
- •Single-character variables
- •Char in action
- •Stuffing characters into character variables
- •Reading and Writing Single Characters
- •The getchar() function
- •The putchar() function
- •Character Variables As Values
- •Unhappily incrementing your weight
- •Bonus program! (One that may even have a purpose in life)
- •The Sacred Order of Precedence
- •A problem from the pages of the dentistry final exam
- •The confounding magic-pellets problem
- •Using parentheses to mess up the order of precedence
- •The computer-genie program example
- •The if keyword, up close and impersonal
- •A question of formatting the if statement
- •The final solution to the income-tax problem
- •Covering all the possibilities with else
- •The if format with else
- •The strange case of else-if and even more decisions
- •Bonus program! The really, really smart genie
- •The World of if without Values
- •The problem with getchar()
- •Meanwhile, back to the GREATER problem
- •Another, bolder example
- •Exposing Flaws in logic
- •A solution (but not the best one)
- •A better solution, using logic
- •A logical AND program for you
- •For Going Loopy
- •For doing things over and over, use the for keyword
- •Having fun whilst counting to 100
- •Beware of infinite loops!
- •Breaking out of a loop
- •The break keyword
- •The Art of Incrementation
- •O, to count backward
- •How counting backward fits into the for loop
- •More Incrementation Madness
- •Leaping loops!
- •Counting to 1,000 by fives
- •Cryptic C operator symbols, Volume III: The madness continues
- •The answers
- •The Lowdown on while Loops
- •Whiling away the hours
- •Deciding between a while loop and a for loop
- •Replacing those unsightly for(;;) loops with elegant while loops
- •C from the inside out
- •The Down-Low on Upside-Down do-while Loops
- •The devil made me do-while it!
- •do-while details
- •The always kosher number-checking do-while loop
- •Break the Brave and Continue the Fool
- •The continue keyword
- •The Sneaky switch-case Loops
- •The switch-case Solution to the LOBBY Program
- •The Old switch-case Trick
- •The Special Relationship between while and switch-case
- •A potentially redundant program in need of a function
- •The noble jerk() function
- •Prototyping Your Functions
- •Prototypical prototyping problems
- •A sneaky way to avoid prototyping problems
- •The Tao of Functions
- •The function format
- •How to name your functions
- •Adding some important tension
- •Making a global variable
- •An example of a global variable in a real, live program
- •Marching a Value Off to a Function
- •How to send a value to a function
- •Avoiding variable confusion (must reading)
- •Functions That Return Stuff
- •Something for your troubles
- •Finally, the computer tells you how smart it thinks you are
- •Return to sender with the return keyword
- •Now you can understand the main() function
- •Give that human a bonus!
- •Writing your own dot-H file
- •A final warning about header files
- •What the #defines Are Up To
- •Avoiding the Topic of Macros
- •A Quick Review of printf()
- •The printf() Escape Sequences
- •The printf() escape-sequence testing program deluxe
- •Putting PRINTFUN to the test
- •The Complex printf() Format
- •The printf() Conversion Characters
- •More on Math
- •Taking your math problems to a higher power
- •Putting pow() into use
- •Rooting out the root
- •Strange Math? You Got It!
- •Something Really Odd to End Your Day
- •The perils of using a++
- •Oh, and the same thing applies to a --
- •Reflections on the strange ++a phenomenon
- •On Being Random
- •Using the rand() function
- •Planting a random-number seed
- •Randoming up the RANDOM program
- •Streamlining the randomizer
- •Arrays
- •Strings
- •Structures
- •Pointers
- •Linked Lists
- •Binary Operators
- •Interacting with the Command Line
- •Disk Access
- •Interacting with the Operating System
- •Building Big Programs
- •Use the Command-Line History
- •Use a Context-Colored Text Editor
- •Carefully Name Your Variables
- •Breaking Out of a Loop
- •Work on One Thing at a Time
- •Break Up Your Code
- •Simplify
- •Talk through the Program
- •Set Breakpoints
- •Monitor Your Variables
- •Document Your Work
- •Use Debugging Tools
- •Use a C Optimizer
- •Read More Books!
- •Setting Things Up
- •The C language compiler
- •The place to put your stuff
- •Making Programs
- •Finding your learn directory or folder
- •Running an editor
- •Compiling and linking
- •Index

Chapter 22: Functions That Actually Funct 279
jerk(500);
This statement calls the jerk() function, which then repeats the mes sage 500 times.
The C-geek vernacular for sending a value of a variable to a function is “passed.” So you pass the value 3 to the jerk() function with this statement:
jerk(3);
The value you send along to a function is called a parameter. It can be said, in a nerdly way, that the jerk() function has one parameter, an integer variable (which is a number).
If you forget to put a value in the jerk() function, you see a Too few parameters type of error. That’s one of the reasons that you prototype functions. If you don’t, and you use jerk() to call the function, your program invariably screws up.
The variable repeat is not a global variable. Instead, it’s a value that’s passed to the jerk function, which that function then uses to do some thing wonderful.
Note that jerk() also retains its own variable, i. Nothing new there.
Avoiding variable confusion (must reading)
You don’t have to call a function by using the same variable name the func tion uses. Don’t bother reading that sentence twice. It’s a confusing concept, but work with me here.
Suppose that you’re in the main() function where a variable named count is used. You want to pass along its value to the jerk() function. You do so this way:
jerk(count);
This line tells the compiler to call the jerk() function, sending it along the value of the count variable. Because count is an integer variable, this strategy works just fine. But, keep in mind that it’s the variable’s value that is passed along. The name count? It’s just a name in some function. Who cares! Only the value is important.
In the jerk() function, the value is referred to by using the variable name repeat. That’s how the jerk() function was set up:
void jerk(int repeat)

280 Part IV: C Level
Whatever value is sent, however it was sent, is always referred to as repeat inside the function.
I bring this concept up because it’s confusing. You can call any function with any variable name. Only inside that function is the function’s own variable name used.
This topic is confusing because of the variable names used by the func tion. You can find this subject in your C manual as well. You see some function listed like this:
int putchar(int c);
This line indicates that the putchar() function requires an integer value, which it refers to as c. However, you can call this function by using any variable name or a constant value. It makes no difference. What’s impor tant, however, is that it must be an integer variable.
Sending More than One
Value to a Function
The tray you use to pass values along to functions is quite large. It can hold more than one item — several, in fact. All you have to do is declare the items inside the function’s parentheses. It’s like announcing them at some fancy diplomatic function; each item has a type and a name and is followed by a lovely comma dressed in red taffeta with an appropriate hat. No semicolon appears at the end because it’s a formal occasion.
For example:
void bloat(int calories, int weight, int fat)
This function is defined as requiring three integer values: calories, weight, and fat. But, they don’t all have to be the same type of variable:
void jerk(int repeat, char c)
Here you see a modification of the jerk() function, which now requires two values: an integer and a character. These values are referred to as repeat and c inside the jerk() function. In fact, the following source code uses said function:
#include <stdio.h>
void jerk(int repeat, char c);
int main()

Chapter 22: Functions That Actually Funct 281
{
printf(“He calls me on the phone with nothing say\n”); printf(“Not once, or twice, but three times a day!\n”); jerk(1,’?’);
printf(“He insulted my wife, my cat, my mother\n”); printf(“He irritates and grates, like no other!\n”); jerk(2,’?’);
printf(“He chuckles it off, his big belly a- heavin’\n”);
printf(“But he won’t be laughing when I get even!\n”); jerk(3,’!’);
return(0);
}
/* The jerk() function repeats the refrain for the value of the repeat variable*/
void jerk(int repeat, char c)
{
int i;
for(i=0;i<repeat;i++)
printf(“Bill is a jerk%c\n”,c);
}
Type the preceding source code. You can start with the BIGJERK4.C source code as a base. You have to edit Line 3 to modify the jerk() function proto type; edit Lines 9, 12, and 15 to modify the way the jerk() function is called (remember to use single quotes); then, redefine the jerk() function itself; and change the printf() statement inside the function so that it displays the character variable c.
Save the file to disk as BIGJERK5.C.
Compile and run the program. The output looks almost the same, but you see the effects of passing the single-character variable to the jerk() function in the way the question marks and exclamation points appear:
He calls me on the phone with nothing say
Not once, or twice, but three times a day!
Bill is a jerk?
He insulted my wife, my cat, my mother
He irritates and grates, like no other!
Bill is a jerk?
Bill is a jerk?
He chuckles it off, his big belly a-heavin’
But he won’t be laughing when I get even!
Bill is a jerk!
Bill is a jerk!
Bill is a jerk!