Week 01 Functions
Week 01 Functions
C++ Function
Example Explained. The syntax of the function is:
4
Call a Function
C++ Function
Example Explained. Inside main, call myFunction():
// Create a function
Declared functions are not executed immediately. They void myFunction()
are "saved for later use", and will be executed later, {
when they are called. }
cout << "I just got executed!";
Call a function, write the function's name followed by two int main()
parentheses () and a “semicolon ;”. {
myFunction(); // call the
In the given example, myFunction() is used to print a function
text (the action), when it is called: return 0;
}
// Function definition
void myFunction()
{
cout << "I just got executed!";
}
7
Call by Value
#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
12
Call by Reference (Example)
If a function with default arguments is called without passing arguments, then the default parameters are used.
However, if arguments are passed while calling the function, the default arguments are ignored.
16
Recursion