what is a function?

A function is a building block of a program. Functions are designed to perform a specific task. Also, functions are reusable, so that a task can be performed multiple times in a program, without having to rewrite the code each time!

syntax

The syntax of a function is:

type name (parameter) { statement }

type is the type of the value returned by the function

name is the identifier by which the function can be called

parameter consists of a type followed by an identifier

statement is the function’s body. This specifies what the function does.

example

Let’s write a function. I need a function that takes the name of a user, that has been stored in a variable, that will be printed with a greeting message:

#include <iostream>
#include <string>

char greeting() {
	std::string name;
	std::cout << "Hello, what is your name? ";
	std::cin >> name;
	std::cout << "Hello ";
	std::cout << name;
	return 0;
}

char main() {
	
}

If this program is run, nothing is printed! This is because the function needs to be invoked (called). This is done by writing the name of the function, with parenthesis, in main:

#include <iostream>

int name;

int message() {
    std::cout << "Hello " << name << " how is your day going?";
}

int main() {
    message();
}

example

Imagine that we need a program that can print a message that can tell you what the temperature is. We could write:

#include <iostream>

int temp;

int main() {
    std::cout << "The temperature is " << temp << " degrees celcius.";
}

This works fine. However, what if the temperature needed to be known multiple times? We would need to rewire the statement:

#include <iostream>

int temp;

int main() {
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
    std::cout << "The temperature is " << temp << " degrees celcius.";
}

This is not efficient and there must be a better way! There is – functions! Using functions we can write:

#include <iostream>

int temperature;

void temperature_now() {
    std::cout << "The temperature is " << temp << " degrees celcius.";
}

int main() {
    temp_now();
}

This program will give the same result as previously but this is more efficient and reusable. Even if we need to check the temperature multiple times, we just call the function each time:

#include <iostream>

int temp;

void temperature_now() {
    std::cout << "The temperature is " << temp << " degrees celcius.";
}

int main() {
    temp_now();
    temp_now();
    temp_now();
    temp_now();
    temp_now();
    temp_now();
    temp_now();
    temp_now();
    temp_now();
    temp_now();
}

function parameters, default and multiple

a block of code that solves a problem. Only executed (invoked) when function called. The body is then executed. Every C++ program contains at least one function, main. When called everything between brackets is executed. You can create your own functions. First write the return type. void means function will not return anything. Then the name for the function with parenthesis, which contain arguments. If left empty function does not receive any arguments. The curly brackets, which contains the body of the function. If we write some code in the body of the function, nothing happens. This is because the function has not been called the therefore the code of the function has not been executed. Functions are called by writing the name of the function, followed by parenthesis.