SlideServe
Menu
Préférences
© SlideServe · v2026

Introduction to Computers and Programming

tahlia avatar
9 diapositives · 263 Views · Aug 23, 2014

This lesson, taught by Professor Avi Rosenfeld, focuses on functions in C programming, including the declaration and definition of functions, along with global and local variable scopes. Illustrated through practical examples, students will learn how to determine the maximum of three integers and the implications of using global variables. Emphasizing good programming practices, this class warns against the potential pitfalls of global variables and demonstrates how to create a simple guessing game, illustrating the importance of function prototypes and variable scope.

Introduction to Computers and Programming - slide 1
01 / 9Introduction to Computers and Programming
1 / 17

Télécharger la présentation

Introduction to Computers and Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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; }