Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What are Functions? | Introduction to Functions
C++ Introduction

What are Functions?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:

cpp

main.cpp

Let's create a function that simplifies the process of converting temperatures from Fahrenheit to Celsius. This is a practical real-life application.

cpp

main.cpp

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.

cpp

main.cpp

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:

cpp

main.cpp

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.

What the name of this function?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 1
course content

Contenido del Curso

C++ Introduction

What are Functions?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:

cpp

main.cpp

Let's create a function that simplifies the process of converting temperatures from Fahrenheit to Celsius. This is a practical real-life application.

cpp

main.cpp

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.

cpp

main.cpp

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:

cpp

main.cpp

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.

What the name of this function?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 1
some-alt