CSC161 2011S Imperative Problem Solving

Reading: Functions in C (K&R 4.1-4.3)

Reading: K&R 4.1-4.3.

Please submit a question by 9 p.m. on Tuesday, 1 March 2011.


Object Code

What exactly is object code? Does it have anything to do with assembly language?

Object code is program code in a form that the computer can understand more-or-less directly. (It sometimes needs a little bit more work before the computer can understand it, but much less work than is required for C.)

What is a source file and what is an object file? What is the difference?

The source file is the .c. It's in a semi-human-readable language, like C. The object file is the translated source file.

I dont really understand the advantage of using object files instead of C files.

You don't have to spend effort recompiling when you don't make changes. While this doesn't matter for small programs, it matters a lot for large programs. For example, compiling the GIMP takes about thirty minutes. If I make one small change, I'd rather not have to wait the whole thirty minutes.

Can you clarify .o a little better on 4.1?

A .o file is code that has been translated from a human-readable language to something closer to the computer's native language.


Do-Nothing Functions

Are do-nothing functions used solely as placeholders during development or do they have another purpose?

How is using dummy, or "a minimal function", useful?

Almost always as placeholders. I tend to call them stubs. When you're developing a big program, and need to write a lot of functions, but want to test it along the way, it's very helpful to just write a simple stub to make sure that everything compiles.

Why would a dummy be necessary other than just a place holder?

It would rarely be useful as something other than a place holder.

What exactly is a "place holder" and why is it useful in program development?

It's something that holds the place for a function you plan to write later. For example, if you know that you'll need to write atoi, but don't want to spend the time right now, you might write


int 
atoi (char str[])
{
  return 42;	// STUB
} // atoi

That way, you can still test other parts of your program.


Miscellaneous

The concept of the "stack" is a bit too abstract for me, especially when the stack is simply a double. I understand stacks as arrays or strings, but a double? I think I'm missing something obvious here.

The stack is an array of doubles.

So C can return ints and floats... not looking forward into the book, will we be writing functions that return non-number answers?

Yes.

What is the difference between getchar() and getch()?

getchar is a standard library function that reads the next character from standard input. getch is a function that K&R define that is similar to getchar, except that you can unread a character (which is sometimes useful).

If we use the command cc main.c getline.c strindex.c (pg 70) and the files all get loaded to one executable file a.out, how will the computer later know which of the three functions we are calling when we call a.out?

When you run a.out, the computer assumes that you want to start with main. You can only have one main.

Are you going to want us to start writing separate functions in different files?

Yes, starting on Friday.

Could you go over either strindex or the Reverse Polish Calculator program in class?

Sure.

Can you omit the braces when declaring a function with a single statement, such as:

double 
square (double d)
  return pow (d, 2);

No.


Vague questions

There are a few things I find confusing in chapter 4.3's example of the Polish calculator. The concept of the calculator is simple enough but there a few syntax things I don't quite understand.

What, in particular?

When it references functions not being able to be split, does this mean a function as a part of program main cannot call on a helper function in another file?

I need more detail on where they say that functions cannot be split.

I don't understand the new content that well, could we possibly go over some quick examples of the uses?

That's somewhat broad. Please identify particular parts that are confusing.


To be covered in a subsequent class

If we write a program that calls pre-existing functions that we've written (and are in seperate files), do we need to compile those files with our program in order for it to work, or is there another way to access them?

We'll cover this on Friday.

I am still confused about how compiling three programs together loads them all into an executable file a.out.

We'll cover this on Friday.

Can you give an example of the use of "external variables" in class? I'm not sure if this is something new or something we've been doing all along.

We'll cover this on Friday.

What is a macro? Is it equivalent to a function?

It is similar to a function, but has some differences. We'll cover macros around March 11.

Can you explain stacks please?

Some time in the near future.

in section 4.3, they use a program that converts a string to an integer. What if the string contains letters?

Can we go over the reverse Polish calculator problem?"

Not right now.

Can you give an example of how atof works?

Not right now.


Ran out of time to answer.

Declaring functions as variables (or that's what it looks like) before the function is defined. For instance on pg. 76 the example code declares

int getop (char []);
void push (double);
double pop (void);

before the function is actually written. From what I understand, this is a way to declare and use these functions before they are actually written, in the chronological order of the code. Without them the compiler would throw an error saying that it doesn't recognize/understand the function. Am I on the right track?

Again going back to the code on pg. 76, the author writes

#define NUMBER '0'

and later uses it in the context of the switch and cases

switch (type) { case NUMBER

How can the variable NUMBER that is defined as '0' encompass all digits?" "Can we go through some of the examples using push and pop in class? Otherwise I understand everything else.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Mar 2 10:52:58 2011.
The source to the document was last modified on Wed Mar 2 10:52:56 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/functions.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu