Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor...

26
Introduction to Programming Using C Modularity

Transcript of Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor...

Page 1: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

Introduction to Programming Using C

Modularity

Page 2: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

2

Contents

Modularity Functions Preprocessor Comments Global variables

Page 3: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

3

Modularity

As programs get bigger, they get harder to manage

This happens with most things and we have developed ways to handle it– Too many things – put them in baskets– Need to play several songs – put them in a

playlist– Got a tough problem – break it into a series of

simpler problems

Page 4: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

4

Modularity

The C language lets us split our code into parts called functions

A function is one way to create a module A module is simply a part of a larger program A module

– Encapsulates part of a program– Can be invoked many times

Page 5: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

5

Functions

A function– Is a block of code – Has a name– Has a series of parameters which can be passed

to it– Can return a value– Can be invoked by other code

Page 6: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

6

The Function piTimes

A simple function to multiply a number times

double piTimes(double n){

return n * 3.141592654;}

Function name Parameter listType returned

Function Body

Indicates value to return

Page 7: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

7

Functions

Every function has– A name which is used to invoke it and must be unique– A parameter list of values passed to the function, which

might be empty– A return type, indicating the type of value returned– A body which performs some calculation using the values

from the parameter list and returns a result

Page 8: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

8

An Interest Calculator

We want to calculate interest– On a give amount of money– At a specific interest rate– For a given number of years

We will show– How to write a function to do this– How to invoke this function

Page 9: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

9

An Interest Calculator

double interest(double principal, double rate, int time)

{

int i;

for(i = 1; i <= time; i = i + 1)

principal = principal * (1 + rate / 100);

return principal;

}

Page 10: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

10

An Interest Calculator

Points of interest– 3 parameters are passed to the function– A local variable, i, is declared within the function– A loop is used to calculate the result– The result is returned via the return statement

Page 11: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

11

Invoking the Calculator

main(){

double start, end, rate;int years;

printf(“Enter amount to invest or 0 to stop\n”);scanf(“%lf”, &start);while(start > 0) {

printf(“Enter rate\n”);scanf(“%lf”, &rate);printf(“Enter years to invest\n”);scanf(“%d”, &years);end = interest(start, rate, years);printf(“Your investment will be worth %.1lf\n”, end);printf(“Enter amount to invest or 0 to stop\n”);scanf(“%lf”, &start);

}}

Page 12: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

12

Compilation

The C language uses a one-pass compiler– This means that it reads the code once only from

start to finish– If you call a function before it sees the function, it

will say the function does not exist

We can overcome this problem by– Using forward declarations for every function in

the program at the start of the program

Page 13: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

13

Forward Declarations

Forward declarations– Use the prototype of the function to tell the

compiler The name of the function The types of the parameters for the function The return type of the function

A prototype is – The function header without the body of the

function

Page 14: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

14

Forward Declarations

The forward declaration for the interest function would be

– double interest(double principal, double rate, int time);

– This is simply the function header without the body

– Notice the semi-colon at the end of the line

* See functiondemo.c

Page 15: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

15

The Preprocessor

Before a C program is compiled it is run through a preprocessor

The preprocessor– Looks for directives starting with # and

Textually includes other files Remembers the definitions of macros Conditionally includes or excludes code from

compilation

– Expands defined macros to their real values

Page 16: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

16

The Preprocessor

CSource

File

CPreprocessor

ModifiedC

SourceFile

CCompiler

Object File

Linker

Library

ExecutableFile

Page 17: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

17

The #include Directive

The first preprocessor directive we will examine is the #include directive

It has the format– #include <filename>

Or

– #include “filename”

These both textuall include another file into your program, replacing the directive

Page 18: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

18

The #include Directive

The first form– #include <filename>– Searches for the file name on the include path– Think of it as searching in the system libraries for

the file name you specified The second form

– #include “filename”– Searches for the file relative to the current

directory

Page 19: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

19

The #include Directive

Both printf and scanf are just regular functions Have you wondered how the compiler knows about

them? We include their prototypes from what is called a

header file– #include <stdio.h>

This file contains the prototypes of many common input and output functions

Page 20: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

20

Comments

Comments are notes inserted into programs to explain to humans what the code is doing

Comments are enclosed between /* and */– /* this is a comment */

Comments can cover several lines Comments are removed by the preprocessor

and are never seen by the compiler It is good practice to comment your programs

Page 21: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

21

Typical Program Layout

Comment describing program and author

#include directives for standard functions

Prototypes of functions other than main()

Definition of the main() function

Defintion of remaining functions with a comment for each

Page 22: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

22

Sample Program

/*****************************************************************//* Display table of investment profits at different rates. *//*****************************************************************/#include <stdio.h>double interest(double principal, double rate, int time);

main(){

double percent;

printf(“Comparison of 5 year returns at different rates\n”);printf(“ Rate Profit per thousand $\n”);printf(“ ------ -----------------------------\n”);for(percept = 3.5; percent < 9.6; percent = percent + 0.5)

printf(“ %.2lf %.2lf\n”, percent, interest(1000.0, percent, 5) – 1000);}

/*************************************************************************//* Calculate value of principal after time years at rate percent *//*************************************************************************/double interest(double principal, double rate, int time){

int i;

for(i = 1; i <= time; i = i + 1)principal = principal * (1 + rate / 100);

return principal;}

Page 23: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

23

Main

main is a function, but …– It is the first function called in your program– It is called by the operating system– It can communicate with the operating system– It has a default return type and optional parameter list

The full prototype for main is– int main(char *argv[])– Technically, we should return 0 from main– We won’t be using it to communicate with the O/S in this

course

Page 24: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

24

Global Variables

We can declare variables in several places– At the top of the file, outside any function

This is a global variable, visible everywhere

– At the start of a function This is a local variable, visible only within the function

– Within a set of curly brackets inside a function This is a variable visible only within the curly brackets in

which it is declared

Page 25: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

25

Global Variables

The scope of a variable is the amount of code which can see the variable

Global variables– Can be seen by any code in the file

Local variables– Can be see throughout a function

Block variables– Can be see throughout the block in which they are declared

Page 26: Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.

26

Global Variables

Many students think global variables are handy because

– You don’t have to pass parameters because they are visible in every function

– You only declare them once However, this is not the case because

– It is very hard to find the functions which modify the global variables

– This makes it Harder to understand how the program works Harder to find mistakes in the program