187k views
3 votes
Develop a void function that takes two parameters, an integer and a string. The function should print the string as many times as given by the integer. For example, with arguments 4 and "Hello" the function would print "Hello" four times. Call the function twice with different arguments. What happens when the argument is negative?

User Pusle
by
7.6k points

1 Answer

5 votes

Final answer:

A void function to print a string multiple times based on an integer argument can be developed using a loop. Positive integer values will result in the string being printed that many times, while negative integers will lead to no output and a message indicating the negative argument.

Step-by-step explanation:

To develop a void function in programming which accepts an integer and a string as parameters and prints the string multiple times based on the integer value, one can use a loop within the function. Here is an example written in Python:

def print_multiple_times(count, message):
if count >= 0:
for _ in range(count):
print(message)
else:
print("The count is negative, no message will be printed.")

Calling the function with arguments 4 and "Hello" will result in:

print_multiple_times(4, "Hello")
# Output:
# Hello
# Hello
# Hello
# Hello

Calling the function with different arguments will produce different outputs. For instance, calling it with arguments 2 and "Goodbye" would print "Goodbye" twice.

print_multiple_times(2, "Goodbye")
# Output:
# Goodbye
# Goodbye

If the function is called with a negative integer, it will not print the message and will instead return a message stating that the count is negative.

User Sabuncu
by
7.7k points