cpp / beginner
Snippet
Function Return Values and Void Functions
Functions can return values using the return type or return nothing using void. The add function takes two integers and returns their sum. The printMessage function takes a string but returns nothing - it simply performs an action.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>int add(int a, int b) {return a + b;}void printMessage(std::string msg) {std::cout << msg << std::endl;}int main() {int result = add(5, 3);std::cout << result << std::endl;printMessage("Hello!");return 0;}
Breakdown
1
int add(int a, int b)
Function that takes two ints and returns an int
2
void printMessage(std::string msg)
Function that takes a string and returns nothing (void)