Functions Summary
In this lesson, you learned that:
- A function is a named piece of code that you write once and can run as many times as you need. When you change a function, the change applies everywhere it's used.
- You define a function with a return type, a name, parentheses with parameters and a body in curly braces:
void printArea(int width, int height) { ... }. A definition alone doesn't run anything. - You call a function with its name, parentheses and a semicolon:
printArea(5, 6);. A program starts in the functionmain, and you call other functions from there. A function must be defined above the place where you call it, otherwise you get the erroruse of undeclared identifier. - A parameter is a variable in the definition, an argument is a value in the call. Arguments go into parameters in order, and their number and types must match the parameters. A parameter can have a default value, but only at the end:
void printPrice(int price, std::string currency = "EUR"). returngives a value back to the place of the call, so you can use the call like a value:int total = add(2, 3) * 2;. The type of the returned value goes before the name of the function.voidmeans that the function returns nothing.std::coutonly shows a value; the program can't work with it. A function that calculates something should return the result. You turn a number into text withstd::to_string().returnends the function right away, and the rest of the function doesn't run. A function that isn'tvoidmust always return a value, otherwise the program stops with an error. The functionmainreturns anint, andreturn 0;at its end means that the program ended fine.- Variables created inside a function, and its parameters, are local: they exist only inside that function, and that applies to
maintoo. Values go into a function through its parameters and come out withreturn. - A parameter gets a copy of the argument, even when it's a vector. When the function changes the parameter, the variable that
mainpassed to it stays the same.
In the next lesson, References and Pointers, you will learn how a function can change a variable that the calling code passed to it, and what addresses in memory are. After that, a final project awaits you, in which you will put together everything you have learned in the course.
Instructions
- 1
Let's practice. Above
main, write a functioncartTotalwith two parameters:pricesof typestd::vector<int>anddiscountof typeintwith the default value0. The function returns the sum of the prices minus the discount. Inmain, below the vectorcart, printcartTotal(cart)andcartTotal(cart, 50), each on its own line. The program prints280and230. - 2
Write a function
priceTextwith a parameterpriceof typeintthat returns the price as text: for0, it returns"free"; otherwise, it returns the price with the currency, for example"230 EUR"for230. Add this line at the end ofmain, beforereturn 0;:std::cout << "To pay: " << priceText(cartTotal(cart, 50)) << std::endl;The program first calls
cartTotaland passes the value it returns topriceText. Below it, add the same line for a customer with a gift voucher, whose discount is280. The program prints:To pay: 230 EUR To pay: free