Given a function pow(int x, int y)
that returns
int x = 2;
int y = 3;
pow(x, y);
cout << pow << endl;
General form is similar to Python:
return_val = function_name(argument1, argument2, ...);
return_val
print(f"Total: ${bill:.2f}")
cout.precision(2);
cout << fixed;
cout << "Total: $" << bill << endl;
cout.precision(n)
sets the number of decimal places to n
fixed
prints a decimal like 308.24
scientific
prints a decimal like 3.08e+02
print(f"Total: ${bill:8.2f}")
print(f"With GST: ${bill*1.05:8.2f}")
#include <iomanip>
cout.precision(2);
cout << fixed;
cout << "Total: $" << setw(8)
<< bill << endl;
cout << "With GST: $" << setw(8)
<< bill*1.05 << endl;
iomanip
library provides more functions for formatting outputsetw(n)
sets the field width to n
charactersprecision
, setw
only affects the next outputJust like variables, C++ requires functions to be declared before they are used
This tells the compiler that the function exists and how it behaves
Similar to a function header in Python:
def func_name(args) -> return_type
return_type func_name(args);
A function declaration is also called a prototype
All function declarations must be placed before main
, and ideally in a separate header file
def func_name(args) -> return_type:
# function body
return return_val
return_type func_name(args) {
// function body
return return_val;
}
void
functionsWhat if you don't want to return anything?
def say_hello() -> None:
print("Hello!")
void say_hello() {
cout << "Hello!" << endl;
}
void
is an explicit return type that means "no return value"return
statement is optionalCaution: Python's return types are just a suggestion, while in C++ they are strictly enforced.
#include <iostream>
#include <cmath>
using namespace std;
double hypoteneuse(double a, double b); // function declaration
int main() {
double a, b;
cout << "Enter the two side lengths of a right angle triangle: ";
cin >> a >> b;
cout << "The hypotenuse length is " << hypoteneuse(a, b) << endl;
return 0
}
double hypoteneuse(double a, double b) { // function definition
return sqrt(a*a + b*b);
}
main
needs to know about the existence of other functions.h
), then #include
itstruct
s)defs.h
defs.cpp
- #include "defs.h"
main.cpp
- #include "defs.h"
defs.h
, keeps main logic clearg++ -c defs.cpp
- compiles defs.cpp
into defs.o
g++ -c main.cpp
- compiles main.cpp
into main.o
g++ -o main main.o defs.o
- links the two object filesmake
Compiling in multiple steps is a tedious process, so we automate it with a makefile
# This is "Makefile". Notice that comments begin with "#"
program: defs.o main.o
g++ main.o defs.o –o program
main.o: main.cpp
g++ -c main.cpp
defs.o: defs.cpp
g++ -c defs.cpp
g++
to compile, run make
(with no arguments)make
. You can ignore the contents of the makefile for now.The curly braces {}
are required to define blocks, but indentation is not
The convention is to indent the contents of a block by 4 spaces
Up to you whether the first {
is on the same line or the next:
int main() {
// ...
}
int main()
{
// ...
}
As usual, be consistent!
As in Python, variables defined in a function (including parameters) are only accessible within that function:
int main() {
int x = 5;
int y = some_func();
return 0;
}
int some_func() {
return x * 2; // Error: x is not defined
}
We'll talk more about scope next lecture
Textbook 4.5, 5.1-5.2
Do an example with hypotenuse
Also do an example with same/different names
Demo setting precision
## IPO Diagrams * IPO stands for **Input, Processing, and Output** * The program as a whole has an IPO diagram, but so does each function * Sometimes drawing it out can be helpful for organizing your thoughts <div data-marpit-fragment> > Example: Hypotenuse function </div>
Example: try to return from void, try to assign void to variable
Show how omitting the declaration doesn't work, but moving the definition does
Also show how variable names aren't required in declaration