Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Passing Custom Structures as Arguments | Function Arguments Specification
C++ Functions
course content

Зміст курсу

C++ Functions

C++ Functions

1. Introduction
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics

bookPassing Custom Structures as Arguments

We can also pass structures (classes) as arguments of the function. We can do it the same as passing simple data type by using structure (class) name as a type specifier inside the function signature:

Like simple data types, we can pass custom structures (classes) by value, pointer, or reference.

cpp

passByValue

cpp

passByPointer

cpp

passByReference

copy
1234567891011121314151617181920212223
#include <iostream> #include <string> // Define a custom structure struct Person { std::string name; int age; }; // Function that takes a Person structure by value void displayPersonInfo(Person p) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } int main() { // Create a Person structure Person person1 = {"Alice", 30}; // Call the function and pass the Person structure by value displayPersonInfo(person1); }

Note

There is another more comfortable way to access the attribute of the structure via pointer: we can use the -> operator instead of (*).. For example:

  • p->name instead of (*p).name;
  • p->age instead of (*p).age

Завдання

Let's create a function to calculate the student's average grade based on his scores in different subjects and display their overall performance.

We had already done this task using a 2-dimensional static array before, but now we will do it with a custom structure.
Your task is to modify the code in the following way:

  • The function must get the student argument by reference.
  • Calculate the student's average score inside the function (variable averageScore)
  • Call the function and pass the created student1 value as an argument of the created function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 7
toggle bottom row

bookPassing Custom Structures as Arguments

We can also pass structures (classes) as arguments of the function. We can do it the same as passing simple data type by using structure (class) name as a type specifier inside the function signature:

Like simple data types, we can pass custom structures (classes) by value, pointer, or reference.

cpp

passByValue

cpp

passByPointer

cpp

passByReference

copy
1234567891011121314151617181920212223
#include <iostream> #include <string> // Define a custom structure struct Person { std::string name; int age; }; // Function that takes a Person structure by value void displayPersonInfo(Person p) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } int main() { // Create a Person structure Person person1 = {"Alice", 30}; // Call the function and pass the Person structure by value displayPersonInfo(person1); }

Note

There is another more comfortable way to access the attribute of the structure via pointer: we can use the -> operator instead of (*).. For example:

  • p->name instead of (*p).name;
  • p->age instead of (*p).age

Завдання

Let's create a function to calculate the student's average grade based on his scores in different subjects and display their overall performance.

We had already done this task using a 2-dimensional static array before, but now we will do it with a custom structure.
Your task is to modify the code in the following way:

  • The function must get the student argument by reference.
  • Calculate the student's average score inside the function (variable averageScore)
  • Call the function and pass the created student1 value as an argument of the created function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 7
toggle bottom row

bookPassing Custom Structures as Arguments

We can also pass structures (classes) as arguments of the function. We can do it the same as passing simple data type by using structure (class) name as a type specifier inside the function signature:

Like simple data types, we can pass custom structures (classes) by value, pointer, or reference.

cpp

passByValue

cpp

passByPointer

cpp

passByReference

copy
1234567891011121314151617181920212223
#include <iostream> #include <string> // Define a custom structure struct Person { std::string name; int age; }; // Function that takes a Person structure by value void displayPersonInfo(Person p) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } int main() { // Create a Person structure Person person1 = {"Alice", 30}; // Call the function and pass the Person structure by value displayPersonInfo(person1); }

Note

There is another more comfortable way to access the attribute of the structure via pointer: we can use the -> operator instead of (*).. For example:

  • p->name instead of (*p).name;
  • p->age instead of (*p).age

Завдання

Let's create a function to calculate the student's average grade based on his scores in different subjects and display their overall performance.

We had already done this task using a 2-dimensional static array before, but now we will do it with a custom structure.
Your task is to modify the code in the following way:

  • The function must get the student argument by reference.
  • Calculate the student's average score inside the function (variable averageScore)
  • Call the function and pass the created student1 value as an argument of the created function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

We can also pass structures (classes) as arguments of the function. We can do it the same as passing simple data type by using structure (class) name as a type specifier inside the function signature:

Like simple data types, we can pass custom structures (classes) by value, pointer, or reference.

cpp

passByValue

cpp

passByPointer

cpp

passByReference

copy
1234567891011121314151617181920212223
#include <iostream> #include <string> // Define a custom structure struct Person { std::string name; int age; }; // Function that takes a Person structure by value void displayPersonInfo(Person p) { std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl; } int main() { // Create a Person structure Person person1 = {"Alice", 30}; // Call the function and pass the Person structure by value displayPersonInfo(person1); }

Note

There is another more comfortable way to access the attribute of the structure via pointer: we can use the -> operator instead of (*).. For example:

  • p->name instead of (*p).name;
  • p->age instead of (*p).age

Завдання

Let's create a function to calculate the student's average grade based on his scores in different subjects and display their overall performance.

We had already done this task using a 2-dimensional static array before, but now we will do it with a custom structure.
Your task is to modify the code in the following way:

  • The function must get the student argument by reference.
  • Calculate the student's average score inside the function (variable averageScore)
  • Call the function and pass the created student1 value as an argument of the created function.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 7
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt