Comp 110 The 'Main' Function

What is the "Main" Function?

The main function, by convention, is the function that starts your program! It is formatted similarly to any other function definition (see below), and is used to call the other functions in the program!

Below are a couple examples of how the main function can be used:

import { print } from "introcs";
//function definition
export let main = async () => {
     print("hello, world");
};
//function call
main();
//when main() is called, all of the code within the main function definition will execute
//in this case, "hello world" will print in the browser!
import { print } from "introcs";
//function definition of introduceMyself
let introduceMyself = (myName: string): string => {
     if(myName === "Kris"){
          return "Hello! I am your professor, " + myName + "!";
     } else {
          return "Hello! I am one of your UTAs, " + myName + "!";
     }
}
//function definition of main function
export let main = async () => {
     print(introduceMyself("Kit"));
};
//function call
main();
//when main() is called, all of the code within the main function definition will execute
//in this case, introduceMyself is called with the argument "Kit"
//"Hello! I am one of your UTAs, Kit!" will print as the main function executes