lecture 12 lecture 12
play

Lecture 12 Lecture 12 2. B inary R epresentation Basics of - PDF document

1. Introduction Lecture 12 Lecture 12 2. B inary R epresentation Basics of Functions 3. H ardw are and S oftw are 4. H igh Level Languages We should all be familiar with mathematical 5. S tandard input and output functions


  1. 1. Introduction Lecture 12 Lecture 12 2. B inary R epresentation Basics of Functions 3. H ardw are and S oftw are 4. H igh Level Languages • We should all be familiar with mathematical 5. S tandard input and output functions 6. Operators, expression and statem ents 7. M aking Decisions f(x) = x 2 - 3x + 5 8. Looping – here f denotes the function 9. A rrays – x is called its argument 10. B asics of pointers – and the expression x 2 - 3x + 5 is an algorithm which 11. S trings describes how to calculate the functions value 12. B asics of functions • Note that x is merely a placeholder and we could 13. M ore about functions write f(z) = z 2 - 3z + 5 or f(p+1) = (p+1) 2 - 3(p+1) + 5 14. Files 14. D ata S tructures 16. C ase study: lottery num ber generator Basics of Functions Basics of Functions • We can also have functions of several variables • So we can write a function which calculates g(x,y,z) = x 2 + y 2 + z 2 the square of a number as – where there are several variables x,y,z float square(float x) • Some mathematical functions have special { return x*x; symbols e.g. e x , log x, sin x, |x| etc but the } principle remains the same • which is like f(x) = x 2 • In C we have a similar idea – this takes a float parameter x (which is the place • Functions take the form of holder) and returns another float value [return type] [function name] ( [argument list…] ) { /* Body of the function where calculations are made*/ return ; /* functions returns some value*/ } /* Functions taking one or more arguments and returning a value */ /* Functions taking one or more arguments and returning a value */ Basics of Functions float power(float x, int n); /* computes x to power n >= 0 */ float power(float x, int n); /* computes x to power n >= 0 */ /* computes x to power n >= 0*/ /* computes x to power n >= 0*/ funct1.c printf("%f to the power %i is %f\n", X, N, power(X, N)); funct1.c printf("%f to the power %i is %f\n", X, N, power(X, N)); /* computes x squared */ /* computes x squared */ /* computes x cubed */ printf("The square of %f is %f\n", X, square(X)); /* computes x cubed */ printf("The square of %f is %f\n", X, square(X)); • The function is “called” as follows printf("The cube of %f is %f\n", X, cube(X)); /* computes x squared */ printf("The cube of %f is %f\n", X, cube(X)); /* computes x squared */ printf("Enter a floating point number: "); printf("Enter a floating point number: "); /* computes x cubed */ float y, z=3.0f; /* computes x cubed */ /* Note: no check is made for n >= 0! */ /* Note: no check is made for n >= 0! */ y=square(z); printf("Enter an integer >= 0: "); printf("Enter an integer >= 0: "); • Notice that we have used Z as the argument /* Example: simple functions */ /* Example: simple functions */ and have assigned the return value to y. /* Function definitions: */ float power(float x, int n) /* Function prototypes: */ /* Function definitions: */ float power(float x, int n) /* Function prototypes: */ float square(float x); float square(float x); float square(float x) • Diagrammatically: float cube(float x); float square(float x) float cube(float x); float cube(float x) return x * x * x; #include <stdio.h> scanf("%f", &X); scanf("%i", &N); float cube(float x) return x * x * x; #include <stdio.h> scanf("%f", &X); scanf("%i", &N); return x * x; while (n > 0) return x * x; float t = 1; while (n > 0) float t = 1; main t *= x; return t; float X; t *= x; return t; float X; The argument z is copied to the copy int N; n--; int N; n--; z x main() parameter (placeholder) x main() { } calculate x*x { } { } { } { } { } copy { } { } { } { } y The return value is copied to y 1

  2. Function Prototypes Function Prototypes • To avoid this C has function prototypes which go • Recall that we must declare variables (primarily near the start, while the function definitions to give them a type) before using them. usually come after main • Similarly we must declare functions before using Identifier not actually needed • eg them prototype float square(float x); • We could do this by putting all functions at the Note the semicolon void main() start of the program (before main) { call y=square(z); • However this has disadvantages } – all the details are given before the general outline definition float square(float x) – two function could call each other so it would be { return x*x; no semicolon impossible to get them in the right order } Functions with no Arguments Functions with no Return Value • Sometimes a function has no argument, but • Again, void is used to denote a lack of return returns a value type for a function. • To cope with this the void keyword is used /* Example: simple functions */ /* Example: simple functions */ /* Function taking an argument but returning no value */ /* Function taking an argument but returning no value */ • The keyword return • VOID is used like a data type but means #include <stdio.h> #include <stdio.h> is used without a void stars(int n); /* print a line of n stars */ “absence of type” or “no type” void stars(int n); /* print a line of n stars */ main() following expression main() { { int rand(void); /* Example: simple functions */ int s; /* Example: simple functions */ int s; /* Function taking no arguments but returning a value */ or can be omitted if /* Function taking no arguments but returning a value */ …… puts("How many stars?"); funct3.c puts("How many stars?"); funct3.c #include <stdio.h> scanf("%i", &s); #include <stdio.h> scanf("%i", &s); #include <stdlib.h> /* for rand */ int i; your lazy/sloppy #include <stdlib.h> /* for rand */ stars(s); stars(s); /* The prototype of rand is int rand(void); */ } i=rand(); /* The prototype of rand is int rand(void); */ } void stars(int n); main() main() • The empty parentheses { void stars(int n) /* print a line of n stars */ { void stars(int n) /* print a line of n stars */ funct2.c …… int n; { int n; funct2.c { while (n-- > 0) ( ) are needed! while (n-- > 0) puts("Some random numbers:"); stars(3); putchar('*'); puts("Some random numbers:"); putchar('*'); for (n = 0; n < 10; n++) putchar('\n'); for (n = 0; n < 10; n++) putchar('\n'); printf("%i ", rand()); return; printf("%i ", rand()); return; putchar('\n'); } putchar('\n'); } } } Functions with no Arguments Multiple Return Statements and no Return Value • It is sometimes convenient to have multiple returns /* Example: simple functions */ /* Example: simple functions */ void print20stars(void); /* Function taking no arguments and returning no value */ /* Function taking no arguments and returning no value */ e.g. in an if…else or switch statement …… #include <stdio.h> funct5.c #include <stdio.h> funct5.c print20stars(); /* Example: simple functions */ void print20stars(void); /* print a line of 20 stars */ /* Example: simple functions */ void print20stars(void); /* print a line of 20 stars */ /* Function with multiple return statements */ /* Function with multiple return statements */ int pins(int tn) /* IC pins lookup */ int pins(int tn) /* IC pins lookup */ main() { main() #include <stdio.h> #include <stdio.h> { { /* Returns the number of pins for an IC of type { /* Returns the number of pins for an IC of type number tn. If the type is not recognised, returns int k; int pins(int tn); /* IC pins lookup */ number tn. If the type is not recognised, returns int k; int pins(int tn); /* IC pins lookup */ zero. */ zero. */ main() for (k = 0; k < 10; k++) main() for (k = 0; k < 10; k++) { switch (tn) switch (tn) print20stars(); { { print20stars(); int ic, p; { int ic, p; } case 555: case 741: case 748: } case 555: case 741: case 748: printf("Enter an IC type number: "); return 8; return 8; printf("Enter an IC type number: "); case 556: case 7400: case 7414: scanf("%i", &ic); case 556: case 7400: case 7414: scanf("%i", &ic); return 14; void print20stars(void) /* print a line of 20 stars */ return 14; void print20stars(void) /* print a line of 20 stars */ p = pins(ic); case 7448: case 7485: case 7448: case 7485: { p = pins(ic); return 16; { return 16; puts("********************"); case 6502: case 6800: case 8085: case 8088: puts("********************"); if (!p) if (!p) case 6502: case 6800: case 8085: case 8088: return; funct4.c printf("IC type %i is not recognised.\n", ic); return 40; return 40; return; funct4.c printf("IC type %i is not recognised.\n", ic); default: else } default: } else return 0; /* easily tested */ printf("IC type %i has %i pins.\n", ic, p); printf("IC type %i has %i pins.\n", ic, p); return 0; /* easily tested */ } } } } } } 2

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend