Meeting #8 (C++: Functions)

Functions

When we start writing longer programs, we might start encountering situations where we are repeating lots of code.

#include <bits/stdc++.h>
using namespace std;

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int main() {
cout << "Printing array:" << endl;
for (int i = 0; i < 10; i++) { // print full array
cout << i << ": " << array[i] << endl;
}
array[0] = 5;
for (int i = 0; i < 10; i++) { // print full array again
cout << i << ": " << array[i] << endl;
}
array[4] = 21;
for (int i = 0; i < 10; i++) { // print full array AGAIN! AAAAAAAH
cout << i << ": " << array[i] << endl;
}
}

What if you wanted to change the code and print the array backwards instead? Then you would have to replace every occurrence of the repeated code. 

In early programming languages, the goto statement allowed the flow of execution to “go to” other sections of code. However, this created a situation known as “spaghetti code”, since having goto statements everywhere made the code much more complicated than it needed to be.

In modern languages, we use functions to avoid these situations and reuse code that already has been written. 

#include <bits/stdc++.h>
using namespace std;

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

void printArray() { // our function to print the array
for (int i = 0; i < 10; i++) {
cout << i << ": " << array[i] << endl;
}
}

int main() {
cout << "Printing array:" << endl;
printArray();
array[0] = 5;
printArray();
array[4] = 21;
printArray(); // soooooo much easier
}

In C++, the function must be declared before using it (unlike some languages such as Java). This means that the following code is incorrect:

#include <bits/stdc++.h>
using namespace std;

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int main() {
cout << "Printing array:" << endl;
printArray(); // ERROR! The function hasn't been declared yet!
}


void printArray() { // our function to print the array
for (int i = 0; i < 10; i++) {
cout << i << ": " << array[i] << endl;
}
}

The general syntax for creating and using a function is this:

type name(parameter1, parameter2, ...) { // function creation
// code here
}

name(parameters); // using a function

Type: This is the type of the variable that you want to return (explained below).

Name: The name of the function (and what it will be referred to in code).

Parameters: Variables that can be given to the function to use (explained below).

Functions also have the ability to take data that is given to them, and manipulate them. For example, I want to have a function that can calculate the factorial (https://en.wikipedia.org/wiki/Factorial) of a number. 

int getFactorial(int num) {
int answer = 1;
for (int i = 2; i <= num; i++) { // calculate factorial
answer = answer * i;
}
return answer; // return a value
}

In this function, our type is int instead of void. This is because we will be returning back a variable that is an integer. Void methods functions do not return values back. That is why we have a return statement at the bottom. The return statement allows us to end the function, and give back a value (the answer).

You will also notice that the brackets () are not empty! We added a parameter to the function. These are variables that are given when the function is called. We typically take them and manipulate them to return a new value (in this case, we got the factorial of num).

Here is a full example of using this function:

#include <bits/stdc++.h>
using namespace std;

int getFactorial(int num) {
int answer = 1;
for (int i = 2; i <= num; i++) { // calculate factorial
answer = answer * i;
}
return answer; // return a value
}

int main() {
cout << "Input a number! We will output the factorial of it." << endl;
int number;
cin >> number;
int answer = getFactorial(number); // called the function with our parameter
cout << answer << endl; // print answer
}

You can also add more than one parameter into a function:

void testFunction(int num, int num2, int num3) {
cout << num << " " << num2 << " " << num3 << endl;
}

This concludes this portion on functions.

Challenge!

Try these problems on DMOJ (be sure to register first):

https://dmoj.ca/problem/ccc13j4

https://dmoj.ca/problem/ccc02s2

Challenge: https://dmoj.ca/problem/sort1

Published by

Computer Club Executives

The account of the Computer Club Executives

Leave a Reply

Your email address will not be published. Required fields are marked *