Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active November 25, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/36fb707be72599a7f4046369141beeec to your computer and use it in GitHub Desktop.
Save kjk/36fb707be72599a7f4046369141beeec to your computer and use it in GitHub Desktop.
#include <iostream>
// function declaration
int add2(int i);
// functin overloading: same name but different arguments
int add2(int i, int j);
// default arguments: i is optional, if not given will be 0
int add3(int i = 0);
// function defintion
int add2(int i) // Data that is passed into (int i) will be referred to by the name i
{ // while in the function's curly brackets or "scope."
int j = i + 2; // Definition of a variable j as the value of i+2.
return j; // Returning or, in essence, substitution of j for a function call to
// add2.
}
int add2(int i, int j) // However, when add2() is called with two parameters, the
{ // code from the initial declaration will be overloaded,
int k = i + j + 2 ; // and the code in this declaration will be evaluated
return k; // instead.
}
int add3(int i)
{
return i + 3;
}
int main()
{
std::cout << "add2(2) : " << add2(2) << "\n";
std::cout << "add2(3, 4): " << add2(3, 4) << "\n";
std::cout << "add3() : " << add3() << "\n";
std::cout << "add3(3) : " << add3(3) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment