Skip to the content. Back to Chapter 1

Sub Programs Activities

Constructs

  1.  global choice
     global cm
     global inches
    
     procedure main()
         while True // iteration/loop
             menu()
     endprocedure
    
     procedure menu()
         print(“1. cm to inches”)
         print(“2. inches to cm”)
         print(“3. Quit”)
         choice = 0
         while choice < 1 or choice > 3
             choice = input(“Enter choice”)
             if choice == "1" then // branching/selection
                 cm_to_in()
             elseif choice == "2" then
                 in_to_cm()
             else
                 quit()
     endprocedure
    
     procedure cm_to_in()
         cm = input("Enter cm: ") // sequence
         inches = cm * 0.393700787
         print (inches&" inches")
     endprocedure
    
     procedure in_to_cm()
         inches = input("Enter inches: ")
         cm = inches * 2.54
         print (cm&" cm")
     endprocedure
    
  2. [above]

  3. for i in {0..25}

  4. while i != 3.14

  5. When the number of iterations needed is known

  6. When the setup for the loop is the same as the body of the loop

Global and Local variables

  1.  #include <stdbool.h>
     #include <stdio.h>
    
     int main() {
         while (true) {
             printf("1. cm -> in\n2. in -> cm\n3. Quit\n>>> ");
             int input = getchar() - '0';
             switch (input) {
             case 1:
                 double user_length;
                 printf("\nEnter length in cm\n>>> ");
                 scanf("%d", &user_length);
                 user_length *= 0.393700787;
                 printf("\n%.2d inches\n");
                 break;
             case 2:
                 double user_length;
                 printf("\nEnter length in inches\n>>> ");
                 scanf("%d", &user_length);
                 user_length *= 2.54;
                 printf("\n%.2d cm\n");
                 break;
             case 3:
                 return 0;
             default:
                 printf("\nInvalid selection.\n");
             }
         }
     }
    
    • Global variables are variables that are accessible from any part of the program.
    • Local variables are variables that are only accessible from the scope they are defined in (or any sub-scope)
    • Scopes are blocks of code with access to a specific set of variables. In Python, only functions and classes create their own scopes. In JavaScript, any pair of curly braces ({}) can create its own scope - but only variables created with let obey this.
    • Scope and local variables can be used to minimise the memory access dangers within your program - preventing unexpected changes to a value due to a subroutine overwriting it - however globals can be used to reduce nesting call parameters - if a function at the bottom of the call stack requires a parameter in order to localise the parameter it must be passed to or generated by the functions leading to it

Parameter passing by value and by reference

Passing by reference

#include <stdio.h>

int substract_deductions(int pay, int percent) {
    return (pay * percent) / 100;
}

void calculate_pay(int *pay) {
    *pay = *pay - subtract_deductions(pay, 22);
}

int main() {
    int pay = 2000;
    calculate_pay(&pay);
    printf("%i\n", pay);
    return 0;
}
#include <stdio.h>

int substract_deductions(int pay, int percent) {
    return (pay * percent) / 100;
}

void calculate_pay(int pay) {
    pay = pay - subtract_deductions(pay, 22);
}

int main() {
    int pay = 2000;
    calculate_pay(&pay);
    printf("%i\n", pay);
    return 0;
}