Course Content
C++ Introduction
C++ Introduction
What are Functions?
Functions are small subroutines that can be called when needed. Each function has a name which it can be called.
Note
The name
main
is already reserved by the C++ language. Therefore, when declaring a function with this name, the compiler will generate an error.
To create a function, you need to:
- define the type of data it will return;
- assign it a name;
- provide a block of instructions (body) within curly braces
{...}
to define its functionality.
For example, let's create a function that outputs the text "c<>definity"
:
Now we can call our new function:
main
#include <iostream> #include <string> std::string nameOfCourses() // type and name of function { // beginning of a body std::string nameOfCourse = "c<>definity"; return nameOfCourse; } // end of a body int main() { std::cout << "Name of course: " << nameOfCourses() << std::endl; }
Let's create a function that simplifies the process of converting temperatures from Fahrenheit to Celsius. This is a practical real-life application.
main
#include <iostream> int FahrenheitToCelsius(int degree) { int celsius = (degree - 32) / 1.8; return celsius; } int main() { int fahr = 80; std::cout << fahr << " F" << " = " << FahrenheitToCelsius(fahr) << " C" << std::endl; }
Note
The function's argument is represented by the variable
degree
, which contains the data the function operates on. In this context, it refers to temperatures in degrees Fahrenheit that need to be converted to degrees Celsius. We will delve into a more detailed explanation of function arguments later on.
The compiler processes our program code sequentially, similar to how a person reads a book, and if it encounters unknown variable or function names, it will produce an error.
To illustrate, let's attempt to invoke a function before it has been defined.
This example throws an error. This is done on purpose.
main
#include <iostream> int main() { int fahr = 80; std::cout << fahr << " F" << " = " << FahrenheitToCelsius(fahr) << " C " << std::endl; } int FahrenheitToCelsius(int degree) //creating function AFTER it calling { int celsius = (degree - 32) / 1.8; return celsius; }
In these situations, it's essential to employ function prototypes.
The purpose of prototyping is to inform the compiler about our function in advance. Creating a prototype is similar to a standard function declaration, but with a subtle difference:
- specify the type of the future function;
- give it a name;
- arguments (if needed);
- put the character of the end of the expression
;
.
Let's add a function prototype to our example that was throwing an error:
main
#include <iostream> int FahrenheitToCelsius(int degree); int main() { int fahr = 80; std::cout << fahr << " F" << " = " << FahrenheitToCelsius(fahr) << " C " << std::endl; } int FahrenheitToCelsius(int degree) { int celsius = (degree - 32) / 1.8; return celsius; }
Note
Prototyping is useful when you are working with a lot of features. To avoid "garbage" in the main file, prototypes and function definitions are moved to third-party files and included in the main file with the
#include
directive.
Thanks for your feedback!