90 likes | 234 Views
Introduction to Computers and Programming. Class 17 Functions Professor Avi Rosenfeld. Example #1. #include <stdio.h> int Maximum(int, int, int); main() { int iMax = 0; iMax = Maximum( 5, 7, 3) ; printf("Maximum is: %d<br>", iMax ); } int Maximum(int x, int y, int z) {
E N D
Introduction to Computers and Programming Class 17 Functions Professor Avi Rosenfeld
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
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
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
/* 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++); }
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
#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); }
#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; }
#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; }