I’ve been watching a C++ tutorial series on Youtube lately, and decided to take some of the basic knowledge that I had of functions, and go into more advanced functions. I was so happy to finally be able to set up a function in an external file, and then pass a variable to that function, and have it be used. Next step will be to manipulate that variable, and pass it back to the main program.
// FunctionExample.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#include "functions.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
string name;
int main()
{
cout << "Please enter your name: ";
cin >> name;
function1(name);
system("pause>0");
}
Code language: C++ (cpp)
//functions.h - This file contains functions declarations for the related cpp file.
#pragma once
int function1(const std::string& name);
Code language: C++ (cpp)
//functions.cpp - User defined functions to be called in the main file.
#include <iostream>
#include "functions.h"
using std::string;
using std::cout;
using std::cin;
using std::endl;
int function1(const string& name)
{
cout << "Hello " << name << ".";
return 0;
}
Code language: C++ (cpp)