7
CSC 253 Lecture 6

CSC 253

Embed Size (px)

DESCRIPTION

CSC 253. Lecture 6. Declarations vs. Definitions. Why do we separate declarations from definitions? What is the rule for placing declarations and definitions? What is “module-based programming” in C?. Our int_math module. Let’s define a file that contains two functions - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 253

CSC 253CSC 253

Lecture 6Lecture 6

Page 2: CSC 253

Declarations vs. Definitions

Declarations vs. Definitions

Why do we separate declarations from definitions?

What is the rule for placing declarations and definitions?

What is “module-based programming” in C?

Why do we separate declarations from definitions?

What is the rule for placing declarations and definitions?

What is “module-based programming” in C?

Page 3: CSC 253

Our int_math moduleOur int_math module

Let’s define a file that contains two functions pow(int base, int exp) fact(int n)

The first function is essentially the same as we wrote in the 3rd week.

Let’s start with the header file.

Let’s define a file that contains two functions pow(int base, int exp) fact(int n)

The first function is essentially the same as we wrote in the 3rd week.

Let’s start with the header file.

Page 4: CSC 253

Our int_math.hOur int_math.h

What idiom do we need to use at the beginning of the file?

Let’s write the rest of the code …

What idiom do we need to use at the beginning of the file?

Let’s write the rest of the code …

Page 5: CSC 253

Out int_math.cOut int_math.c

Let’s check parameters for validity …

Let’s code fact() recursively. Suppose we compile int_math.c;

what will happen?

Let’s check parameters for validity …

Let’s code fact() recursively. Suppose we compile int_math.c;

what will happen?

Page 6: CSC 253

Separate CompilationSeparate Compilation

We can compile int_math.c even without writing a main program. We just use the -c switch on our call

to gcc This produces an object file that can

be used later.

We can compile int_math.c even without writing a main program. We just use the -c switch on our call

to gcc This produces an object file that can

be used later.

Page 7: CSC 253

The Main ProgramThe Main Program

Now let’s write the main program … (exercise)

We can run the program by listing our source file, along with the precompiled object file.

We can also compile both files together …

Now let’s write the main program … (exercise)

We can run the program by listing our source file, along with the precompiled object file.

We can also compile both files together …