CH 14 Flashcards

Writing and using Functions

1
Q

What headers?

A

stdio. h

ctype. h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2 #include ctype.h

A

declares ,functions ,testing ,mapping ,characters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

1 #include studio.h

A

defines three variable types,

macros,functions for input and output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Functions for Later

A
//  forward declaration  
5 int can_print_it( char ch); 
6 void print_letters( char arg[]);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define can_print_it, which simply returns the truth value (0 or 1) of
isalpha(ch) || isblank(ch) back to its caller, print_letters.

A

32 int can_print_it( char ch)
33 {
34 return isalpha(ch) || isblank(ch);
35 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Finally, make the whole chain of functions go.

A
37 int main( int argc, char * argv[]) 
38 { 
39 print_arguments(argc, argv); 
40 return 0 ;
41 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

void?

A

A pointer to void can be converted to any pointer type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Every program is starting with what definition?

A

main(int argc, char *argv[])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

minimum possible value of argc

A

is 1 which is the execution command itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

argv[0] holds

argv[1] holds

A

argv[0] the string ./mymainprogram

argv[1] is next argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A
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 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A
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 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

forward declarations means

A

TYPE VAR_NAME( TYPE OTHER_VAR);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Are these two Variables
5 int can_print_it
6 void print_letters
or functions called from the header?

A

Variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

isalpha(int c)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

isblank()

A

isblank() considers blank characters the tab character (‘\t’) and the space character (‘ ‘).

17
Q

isblank()

A

int isblank(int ch)

18
Q

what are these?
5 int can_print_it( char ch);
6 void print_letters( char arg[]);

A

functions

19
Q

explain this program

A

To play with this program, you just feed it different command line arguments, which get passed
through your functions.

20
Q

a deeper explanation of forward declarations

A

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

21
Q

Why the new header file?

A

cstyle.h gives us

so we can gain access to isalpha and isblank.

22
Q

Ref loop covering print_arguments

8 void print_arguments( int argc, char * argv[])

A
argv pointing at Array returning a char
and
argc returns an Int
called by
print_arguments
returning
a void
This is a function
23
Q
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 }
A
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
24
Q

Syntax of the if statement in

24 if (can_print_it(ch)) { 
25 printf( "'%c' == %d " , ch, ch); 
26 }
A

if ( statement is TRUE )

Execute this line of code

25
Q

when is can_print_it true?

A

when it is non zero and non null

26
Q
what is happening here
24 if (can_print_it(ch)) { 
25 printf( "'%c' == %d " , ch, ch); 
26 }
A

Variable ch is being tested if true then the decimal version(int) and the single letter it translates into (char)

27
Q

8 void print_arguments( int argc, char * argv[])
what is this bit called?
»»»»char * argv[]

A

pointer declaration

type *identifier;

28
Q

how to think about 1 dimensional arrays

A

Think about arrays like this:
[][][][][][]
Each of the bracket pairs is a slot(element) in the array, and you can put information into each one of them.
a group of variables side by side.

29
Q

a compiler needs to know about a function

A

compiler what the function will return, what the function will be called, as well as what arguments the function can be passed

30
Q

arguments in a function

A

there can be more than one argument passed to a function or none at all (where the parentheses are empty), and it does not have to return a value.

31
Q
How to think about function
int mult ( int x, int y );
A

the function mult will accept two arguments,
both integers
it will return an integer.
Do not forget the trailing semi-colon.

32
Q

How to think about function

A

What will it return? Void
Call it a functional name
(How is this Argument returned
What arguments it needs)

33
Q
what is it thinking here
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];
A

it is called print letters
arg[] is variable
char returns a character
( char arg[]) gets replaced with print_letters(argv[i]);
it gets replaced with whats in the brackets
just needs to be same types
is getting spat into char ch for later use

34
Q

Functions that don’t return values have a type of ?

A

void.