Last active
November 25, 2019 09:53
-
-
Save kjk/36fb707be72599a7f4046369141beeec to your computer and use it in GitHub Desktop.
example for https://www.notion.so/essentialbooks/Function-8ed3177ded73416a960f6580f5937e0b (made with https://codeeval.dev)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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