1 / 9

Introduction to Computers and Programming

Introduction to Computers and Programming. Class 17 Functions Professor Avi Rosenfeld. Example #1. #include &lt;stdio.h&gt; int Maximum(int, int, int); main() { int iMax = 0; iMax = Maximum( 5, 7, 3) ; printf(&quot;Maximum is: %d<br>&quot;, iMax ); } int Maximum(int x, int y, int z) {

tahlia
Download Presentation

Introduction to Computers and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computers and Programming Class 17 Functions Professor Avi Rosenfeld

  2. Example #1 #include <stdio.h> int Maximum(int, int, int); main() { int iMax = 0; iMax = Maximum( 5, 7, 3) ; printf("Maximum is: %d\n", iMax ); } int Maximum(int x, int y, int z) { int max = x; if (y > max) max = y; if (z > max) max = z; return max; } /* end maximum function */ Function Prototype: this function takes three ints, and returns one int. Function Definition Return statement

  3. Global v. Local Variables • Global: • A variable declared outside any function • Can be referenced by any function in the program • Local: • A variable declared inside a function • Can only be referenced within that function. • If you have a local variable in a function and a global variable with the same name, the local one is used

  4. Example /* This program demonstrates global variables and scope */ #include <stdio.h> void a (void); void b (void); int x = 1; /* Global Variable */ int main () { printf ("In main, x equals: %d\n", x); a(); b(); printf ("In main, x equals: %d\n", x); return 0; } void a () { int x = 100; printf ("In function (a), x equals: %d\n", x); } void b () { printf ("In function (b), x equals: %d\n", x++); } If you have a local variable and a global variable with the same name, the local one is used. In main, x equals: 1 In function (a), x equals: 100 In function (b), x equals: 1 In main, x equals: 2

  5. /* This program demonstrates global variables and scope */ #include <stdio.h> void a (void); void b (void); int x = 1; /* Global Variable */ int main () { printf ("In main, x equals: %d\n", x); a(); b(); printf ("In main, x equals: %d\n", x); return 0; } void a () { int x = 100; printf ("In function (a), x equals: %d\n", x); } void b () { printf ("In function (b), x equals: %d\n", x++); }

  6. Avoid Global Variables! • Now that you know about global variables, never use them! • Why? • If you use a global variable, any function can modify it • This makes it extremely hard to track down problems • Undermines the modularity of your programs

  7. #include <stdio.h> #include <time.h> #include <stdlib.h> #define computerNumber 73; void main() { int count = 0, user, computer=computerNumber; do { printf("Please guess the computer's number\n"); scanf("%d",&user); count++; if (user < computer) printf("You guessed too low, try again\n"); if (user > computer) printf("You guessed too high, try again\n"); }while(user!=computer); printf("You got it! It only took you %d tries\n", count); }

  8. #include <stdio.h> #include <time.h> #include <stdlib.h> int ComputerNumber(); void main() { int count = 0, user, computer=ComputerNumber(); do { printf("Please guess the computer's number\n"); scanf("%d",&user); count++; if (user < computer) printf("You guessed too low, try again\n"); if (user > computer) printf("You guessed too high, try again\n"); }while(user!=computer); printf("You got it! It only took you %d tries\n", count); } int ComputerNumber() { srand(time(0)); return 1 + rand() % 100; }

  9. #include <iostream.h> #include <stdio.h> int GPA(char); void main() { int count = 0, total = 0, tempcredit; char tempgrade; for (int i = 0; i < 5; i++) { printf("Type grade #%d followed by a credit value\n", i + 1); cin >> tempcredit >> tempgrade; total+=GPA(tempgrade); count+=tempcredit; printf("You typed %c and %d\n", tempgrade, tempcredit); } printf("Your GPA for your %d credits is %.2f\n", total, float(total)/count); } int GPA (char x) { if (x == 'a' || x == 'A') return 4; if (x == 'b' || x == 'B') return 3; if (x == 'c' || x == 'C') return 2; if (x == 'd' || x == 'D') return 1; return 0; }

More Related