Function and Array
Revision
Function
A function is a group of statements that together
perform a task.
Every C++ program has at least one function,
which is main(), and all the most trivial programs can
define additional functions.
A function declaration tells the compiler about a
function's name, return type, and parameters.
A function definition provides the actual body of the
function
Create a Function myFunction() is
Syntax the name of the
void myFunction() function
void means that
{
the function does
// code to be executed
not have a return
} value.
Inside the function
(the body), any
code that defines
what the function
should do.
Call a Function
Declared functions are not executed
immediately.
They are "saved for later use", and will
be executed later, when they are called.
To call a function, write the function's
name followed by two parentheses () and a
semicolon ;
Example1
void myFunction() { // Create a function
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
Example2
A function can be called multiple times:
void myFunction() {
cout << "I just got executed!\n";
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
Function Cont.…..
Ifa user-defined function, such as
myFunction() is declared after the main()
function, an error will occur:
A function declaration must be above
main(), and function definition must be
below main().
Example 3
int main() {
myFunction();
return 0;
}
void myFunction() {
cout << "I just got executed!";
}
Parameters and Arguments
Information
can be passed to functions
as a parameter.
Parameters act as variables inside the
function.
Parametersare specified after the
function name, inside the parentheses.
Example 4
#include <iostream>
#include <string> When a parameter is
using namespace std; passed to the function,
void myFunction(string Lname) { it is called an
cout << Lname << " Abebe\n";
argument.
Parameter – Lname
}
Arguments - Lemma,
int main() { Haile and Nahom
myFunction(“Lemma");
myFunction(“Haile");
myFunction(“Nahom");
return 0;
}
Default Parameters
Default parameter value can be used with
equals sign (=).
Ifwe call the function without an
argument, it uses the default value
A parameter with a default value, is often
known as an "optional parameter".
Example 5
#include <iostream>
#include <string>
using namespace std;
void myFunction(string country = “Ethiopia") {
cout << country << "\n";
Optional parameter –
}
country
int main() { Default value - Ethiopia.
myFunction(“China"); //Outputs
myFunction("India"); China
myFunction(); India
Ethiopia
return 0;
}
Multiple Parameters
Example6
#include <iostream>
#include <string>
using namespace std;
void myFunction(string Lname, int age) {
cout << Lname << " Abebe. " << age << " years old. \n";
}
int main() {
myFunction(“Nahom", 3);
myFunction(“Jeri", 14); Outputs
myFunction(“Haile", 30); // Nahom Abebe. 3 years old.
return 0; // Jeri Abebe. 14 years old.
// Haile Abebe. 30 years old.
}
Keywords
Thevoid keyword, indicates that the function
should not return a value.
Toreturn a value in a given function, we can
use a data type (such as int, string, etc.)
instead of void, and use the return keyword
inside the function:
Example- 6
int myFunction(int x) {int myFunction(int x, int y)
return 5 + x; {
} return x + y;
}
int main() { int main() {
cout << myFunction(3); int z = myFunction(5, 3);
return 0; cout << z;
return 0;
}
}
// Outputs 8 (5 + 3)
Function Overloading
With function overloading, multiple
functions can have the same name with
different parameters:
Multiplefunctions can have the same name
as long as the number and/or type of
parameters are different.
Example - 8
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int plusFuncInt(int x, int y) { int plusFunc(int x, int y) {
return x + y; return x + y;
} }
double plusFuncDouble(double x, double plusFunc(double x, double y) {
double y) { return x + y;
return x + y; }
} int main() {
int main() { int myNum1 = plusFunc(8, 5);
int myNum1 = plusFuncInt(8, 5); double myNum2 = plusFunc(4.3,
double myNum2 = 6.26);
plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
cout << "Double: " << myNum2;
return 0;
return 0;