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"> 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">
Download presentation
Presentation is loading. Please wait.
2
Introduction to Computers and Programming Class 17 Functions Professor Avi Rosenfeld
3
2 Example #1 #include 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
4
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
5
4 /* This program demonstrates global variables and scope */ #include 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 Example
6
/* This program demonstrates global variables and scope */ #include 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++); }
7
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
8
#include #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); }
9
#include 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; }
10
#include 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; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.