csc 253
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 PresentationTRANSCRIPT
CSC 253CSC 253
Lecture 6Lecture 6
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?
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.
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 …
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?
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.
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 …