Module: (C++) Subroutines: Procedures and Functions - 2


Problem

1/11

subroutine-function

Theory Click to read/hide

A function is a subroutine that returns a result (number, character string, etc.)

Imagine that you have ordered a product from an online store. From a programming point of view, you have called a certain subroutine, and unlike a procedure, this subroutine must return a result - deliver the product you ordered. These subroutines are called functions.
A function is formatted in exactly the same way as a procedure. The only difference from a procedure is the presence of a special operator returnafter which the value to be returned to the main program is written.

A function that returns the arithmetic mean of two integers would look like this: float average(int a, int b) { float avg = (a + b) / 2.; return aug; } It remains to understand how to call this function in the main program:
You shouldn't call a function the same way you call a procedure: average(10, 5); The value returned by the function will be lost. It is as if the goods from the online store were not given to anyone, but thrown away. It is unlikely that the customer will like it.

It's more correct to save the result in a variable (or print it to the screen): a = average(10, 5); cout << average(10, 5);

Problem

Write a function that calculates the sum of the digits of a natural number.
Using this function, write a program that, among 5 entered natural numbers, finds the number with the maximum sum of digits. If there are several such numbers, print the larger number
 
Examples
# Input Output
1 15
234
11
9
111112
234