CH 14 Flashcards
Writing and using Functions (34 cards)
What headers?
stdio. h
ctype. h
2 #include ctype.h
declares ,functions ,testing ,mapping ,characters.
1 #include studio.h
defines three variable types,
macros,functions for input and output
Functions for Later
// forward declaration 5 int can_print_it( char ch); 6 void print_letters( char arg[]);
Define can_print_it, which simply returns the truth value (0 or 1) of
isalpha(ch) || isblank(ch) back to its caller, print_letters.
32 int can_print_it( char ch)
33 {
34 return isalpha(ch) || isblank(ch);
35 }
Finally, make the whole chain of functions go.
37 int main( int argc, char * argv[]) 38 { 39 print_arguments(argc, argv); 40 return 0 ; 41 }
void?
A pointer to void can be converted to any pointer type
Every program is starting with what definition?
main(int argc, char *argv[])
minimum possible value of argc
is 1 which is the execution command itself
argv[0] holds
argv[1] holds
argv[0] the string ./mymainprogram
argv[1] is next argument
For loop covering print_arguments
Define the print_arguments function, supply it with the command line info
Start the counter on argc
Redefine the print_letters function
8 void print_arguments( int argc, char * argv[]) 9 { 10 int i = 0 ; 11 12 for (i = 0 ; i < argc; i ++ ) { 13 print_letters(argv[i]); 14 } 15 }
For loop covering print_letters
Define the print_arguments function, supply it with a array variable
Start a counter for that array variable
spit the array variable into a char variable
pass the variable through the can_print_it function
then print the result
17 void print_letters( char arg[]) 18 { 19 int i = 0 ; 20 21 for (i = 0 ; arg[i] != '\0' ; i ++ ) { 22 char ch = arg[i]; 23 24 if (can_print_it(ch)) { 25 printf( "'%c' == %d " , ch, ch); 26 } 27 } 28 29 printf( "\n" ); 30 }
forward declarations means
TYPE VAR_NAME( TYPE OTHER_VAR);
Are these two Variables
5 int can_print_it
6 void print_letters
or functions called from the header?
Variables
isalpha(int c)
Explanation: Even though it accepts an int, it really deals with characters. Based on the ASCII value of the character it will determine if it is an alphabetical character or not.
isblank()
isblank() considers blank characters the tab character (‘\t’) and the space character (‘ ‘).
isblank()
int isblank(int ch)
what are these?
5 int can_print_it( char ch);
6 void print_letters( char arg[]);
functions
explain this program
To play with this program, you just feed it different command line arguments, which get passed
through your functions.
a deeper explanation of forward declarations
defined functions the same
help C out by telling it
if you’re going to use functions
it hasn’t encountered yet in the file
Why the new header file?
cstyle.h gives us
so we can gain access to isalpha and isblank.
Ref loop covering print_arguments
8 void print_arguments( int argc, char * argv[])
argv pointing at Array returning a char and argc returns an Int called by print_arguments returning a void This is a function
Ref loop covering print_arguments How this function works 9 { 10 int i = 0 ; 11 12 for (i = 0 ; i < argc; i ++ ) { 13 print_letters(argv[i]); 14 } 15 }
Set integer to 0 Initialise function at 0 Test if it is less that argc Run Code Kicks off at argv 0 up to /0 delivers the print_letters function to the program Go up in an incriment of 1
Syntax of the if statement in
24 if (can_print_it(ch)) { 25 printf( "'%c' == %d " , ch, ch); 26 }
if ( statement is TRUE )
Execute this line of code