1.3 Functions & Main - NO-skcaj/FRC-Cpp-Crash-Course GitHub Wiki

Main

To run our code we need to put it all within the main function. The main function is called by the compiler when your code executes; you can use this to jump start the rest of your code.

Let's look at an example:

// creates the main function
int main()
{
    return 0; // 0 is a successful return code
}

Running this code outputs nothing but it does succeed without errors because it returns an int (as required by the declaration) and returns a 0.

#include <iostream> // enables us to use the "cout" function
using namespace std;

// Declares and Defines the function.
int main()
{
    // cout is a function that outputs data
    cout << "Hello World" << endl; // outputs "Hello World" and starts a new line
    return 0;
}

What is a function?

Similar to function in math, a function can take inputs, work on them, and then return something.

In programming, our definition widens: A function is also a block of code that you can execute. That means that, in addition to taking inputs and returning values, it can call other functions that might interact with hardware.

Its useful to think of functions in 1 of 2 ways:

  • Logic & Math: It works with numbers doing "business logic" and other logical functions like variable declaration.
  • Application: it interacts with hardware or libraries

Let's make a function together. We want to make a function that doubles a number that it takes as an input. First we have the declaration. We can define it using its return type, name, and list of parameters.

// of return type int
// called "doubleInt"
// requires the "arg" parameter of type int
int doubleInt(int arg);

This outlines what we want, but we need to define its behavior. Without defining it, the compiler won't know what it does or what it should do. To define it, we use this syntax.

int doubleInt(int arg) // notice how this is similar to the declaration
{
    // put your implementation here!
}

So that's the syntax, but it won't work. When we declare a function as having a return type, the compiler requires the function to return the value. So, lets add the implementation. We just need to multiply arg by 2, and return it.

int doubleInt(int arg)
{
    // Notice that we use the "return" keyword, followed by an expression.
    // When the function is called it becomes equivalent to that expression.
    return arg * 2;
    // So, the code runs, and we get an output
}

Awesome, so now how do we use it? We can call the function by accessing its name followed by the () operator. I.e: doubleInt(). but we're missing something, doubleInt requires a parameter of type int. So we pass whats called an argument into the function, we put it in between the parenthesis, so: doubleInt(2). Now, with our finished function call, we can say that it represents an expression. Let's look at an example:

#include <iostream> // cout
using namespace std; // simplify cout

// Declares and defines the function
// int is the return type
// arg is the parameter of type int
int doubleInt(int arg)
{
    // multiplies arg by 2 and returns.
    return arg * 2;
}

// All code is in main
int main()
{
    int x = doubleInt(2); // 2 is the argument into doubleInt
    cout << x << endl;
}

What will this program output? We start at main and the declaration/definition of x. We assign x to doubleInt(2), so lets unpack the function call. doubleInt takes 1 parameter that is then doubled and returned, as modeled here return arg * 2. So we know that the 2 that we pass in will be multiplied (*) by 2, becoming 4, and returned. So, we can replace doubleInt(2) with the equivalent expression 4, x == 4. Then we use cout to output that 4.

tl;dr the code outputs 4.

So... what if we didn't want to use a parameter? Another way of modeling this code is by defining x out of the main function, and allow it to be accessed by all the functions. This is called putting the variable into "Global Scope", which we'll dive further into later on.

#include <iostream> // cout
using namespace std; // simplify cout

// Declaring x in the global scope
int x;

int doubleX()
{
    // multiplies x by 2 and assigns that to x.
    x = x * 2;
}

// All code is in main
int main()
{
    x = 2; // defining it as 2, when we first define a variable its called initialization.
    doubleX();
    cout << x << endl;
}

This code achieves the same goal, but in a different way. This is the beauty, and the fun of writing code: we can use multiple different solutions to solve a problem, and we can see which one is the best.

To summarize:

  • All code is called from the main function
  • We can declare and define our own functions
  • These functions can return expressions
  • Functions can input parameters
⚠️ **GitHub.com Fallback** ⚠️