Functions and Methods in C++

Functions are reusable pieces of code. Are you writing the same code again and again in a repetitive way? Consider replacing the repetition with function calls. A function call is when you use a function. A function can accept inputs into it, called arguments. A piece of code that uses a function is called a caller. Functions can optionally return values. A function is essentially a mini-program within a bigger program.

Functions and methods are similar, and depending on the language you’re using, they might be used interchangeably, or maybe only one term or the other will be used. But for the most part, the difference between a function and a method is that a function can typically be on its own, like sayHello(), whereas a method is typically associated with an object or class, such as someObject.sayHello() or “hello”.toCharArray(). This is not always the case though. someString.toCharArray() is a string method that Java has that I wish C++ had.

C++ calls everything a function, regardless of whether it’s associated with classes/objects.

Functions in C++:

#include <iostream>

#include <time.h>

using namespace std;

int squared(int arg){

return arg * arg;

}

int exponent(int num, int power) {

int result = 1;

if (power == 0) {

result = 1;

} else if (power < 0) {

result = -1;

cout << “this function can’t deal with negative exponents” << endl;

//limitation of this simple example

} else {

for (int i = 1; i <= power; i++) {

result *= num;

}

}

return result;

}

int add(int arg1, int arg2) {

int sum = arg1 + arg2;

return sum;

}

void sayHello(string name){

cout << “Hello, ” << name << endl;

}

bool isEven(int arg) {

return arg % 2 == 0;

}

void clearRandom() {

srand(time(NULL));

}

int rollD6(){

int roll = 0;

roll = (rand() % 6) + 1;

//the above line is how you get a random number from 1-6

//because a number n % 6 can only be 0-5, and adding one to it

//makes the range 1-6

//which is like a 6-sided die

return roll;

}

int main() {

clearRandom();

int base = rollD6();

int power = rollD6();

sayHello(“your name here”);

cout << base << ” to the power of ” << power << ” is “;

cout << exponent(base, power) << endl;

return 0;

}

C++ class functions (similar to “methods” in other languages):

The following code example is a person class’s Person.cpp file. In C++, you might take a class and split it into two files: one is the header, which ends in .h, and contains prototypes, or unfinished stuff. It’s basically a barebones layout. On the other hand, a class .cpp file, such as Person.cpp, will contain the implementation, which is the more fleshed out details of the class:

#include <iostream>

#include “Person.h”

using namespace std;

Person::Person(string firstName, string lastName) {

this->firstName = firstName;

this->lastName = lastName;

}

Person::Person(string first) {

firstName = first;

lastName = “”;

}

Person::Person() {

firstName = “Default”;

lastName = “Name”;

}

string Person::getFullName(){

return firstName + ” ” + lastName;

}

string Person::getFirstName() {

return firstName;

}

string Person::getLastName() {

return lastName;

}

C++ function prototypes:

Here are some function prototypes from the Person class’s header file (Person.h):

private:

string firstName;

string lastName;

public:

Person(string firstName, string lastName);

Person(string first);

Person();

string getFullName();

string getFirstName();

string getLastName();

The above code is not the entire code for the header. That will be covered more in a later section in this chapter.

Prototypes and implementations tend to trip people up, so you’re not alone if it’s confusing to you. There’s a much more in-depth explanation of all of this in section 6.6, which focuses more on classes. 6.3 is just functions though.

Pure virtual functions in C++:

virtual void example() = 0;

Pure virtual functions are associated with abstract classes, which are covered more in section 6.6.

← Previous | Next →

C++ Topic List

Main Topic List

Leave a Reply

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