Download presentation
Presentation is loading. Please wait.
Published byArthur Stevenson Modified over 9 years ago
1
Introduction to Programming Using C Modularity
2
2 Contents Modularity Functions Preprocessor Comments Global variables
3
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
4
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
5
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
6
6 The Function piTimes A simple function to multiply a number times double piTimes(double n) { return n * 3.141592654; } Function nameParameter list Type returned Function Body Indicates value to return
7
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
8
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
9
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; }
10
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
11
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); }
12
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
13
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
14
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
15
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
16
16 The Preprocessor C Source File C Preprocessor Modified C Source File C Compiler Object File Linker Library Executable File
17
17 The #include Directive The first preprocessor directive we will examine is the #include directive It has the format – #include Or – #include “filename” These both textuall include another file into your program, replacing the directive
18
18 The #include Directive The first form – #include – 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
19
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 This file contains the prototypes of many common input and output functions
20
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
21
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
22
22 Sample Program /*****************************************************************/ /* Display table of investment profits at different rates. */ /*****************************************************************/ #include 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; }
23
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
24
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
25
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
26
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.