Challenge: Passing Structs to Functions
When you pass a struct by reference to a function, any changes made inside the function affect the original struct, allowing you to modify its data directly. This is especially useful for updating specific fields, such as changing the age of a Person struct.
Swipe to start coding
Complete the function so that it updates the age of a Person struct passed by reference.
- Set the
agemember of the givenPersonto the value provided in thenewAgeparameter. - After calling
updateAge, print the updated age using the template string: "Updated age: {age}".
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you show an example of passing a struct by reference?
What is the syntax for passing a struct by reference in different programming languages?
Are there any drawbacks to passing structs by reference?
Awesome!
Completion rate improved to 8.33
Challenge: Passing Structs to Functions
Svep för att visa menyn
When you pass a struct by reference to a function, any changes made inside the function affect the original struct, allowing you to modify its data directly. This is especially useful for updating specific fields, such as changing the age of a Person struct.
Swipe to start coding
Complete the function so that it updates the age of a Person struct passed by reference.
- Set the
agemember of the givenPersonto the value provided in thenewAgeparameter. - After calling
updateAge, print the updated age using the template string: "Updated age: {age}".
Lösning
Tack för dina kommentarer!
single