How to put this part of my program into a function

In summary, In summary, AlephZero's program lets a user enter numbers and then gives them back reversed. DavidSnider's program prints the numbers in reverse order. jtbell's program prompts the user for how many numbers to enter and then reads in those numbers.
  • #1
leroyjenkens
616
49
Hello, here is my program. It let's a user put in numbers and then gives them back to the user reversed. Can someone help me separate this program into functions, with the part that reverses the numbers being in its own function?

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int readNum;
    int numbers[50];

    printf("How many numbers would you like to enter? ");
    scanf("%d", &readNum);

    printf("\nEnter your numbers: ");

    for (int i = 0; i < readNum; i++)
        scanf("%d", &numbers[i]);

    printf("\nYour numbers reversed are: ");
    for (int i = readNum - 1, numPrinted = 0; i >= 0; i--)
    {
        printf("%d", numbers[i]);
        if (numPrinted < 9)
            numPrinted++;
            else{printf("\n");
                numPrinted = 0;}
    }
    return 0;
}

I'm still struggling figuring out how to separate programs I make into functions.
 
Technology news on Phys.org
  • #2
Step 1: Isolate the section of code you want to factor out
Code:
printf("\nYour numbers reversed are: ");
    for (int i = readNum - 1, numPrinted = 0; i >= 0; i--)
    {
        printf("%d", numbers[i]);
        if (numPrinted < 9)
            numPrinted++;
            else{printf("\n");
                numPrinted = 0;}
    }

Step 2: Determine what variables are requried as input
You will need the numbers[] array and readNum

Step 3: Determine what information you need to be returned once the process is complete
None.

Step 4: Construct the signature of your function
void PrintArrayInReverse(int numbers[], int readNum) {

}

Step 5: Refactor
Code:
#include <stdio.h>
#include <stdlib.h>

void PrintArrayInReverse(int numbers[], int readNum) {
int numPrinted = 0;
printf("\nYour numbers reversed are: ");
    for (int i = readNum - 1, numPrinted = 0; i >= 0; i--)
    {
        printf("%d", numbers[i]);
        if (numPrinted < 9)
            numPrinted++;
            else{printf("\n");
                numPrinted = 0;}
    }
}

int main()
{
    int readNum;
    int numbers[50];

    printf("How many numbers would you like to enter? ");
    scanf("%d", &readNum);

    printf("\nEnter your numbers: ");

    for (int i = 0; i < readNum; i++)
        scanf("%d", &numbers[i]);

    PrintArrayInReverse(numbers,readNum);
    return 0;
}
 
  • #3
Just curious, is this C or C++?
 
  • #4
its C
 
  • #5
Thank you. That step by step explanation helped.

Yes it's C.
 
  • #6
leroyjenkens said:
I'm still struggling figuring out how to separate programs I make into functions.

One way is to start with a "program" that isn't in C, but solves the problem in a simple logical way. (Or to give it a better name, an "algorithm"). For example
Code:
int main()
{
    read_some_numbers_from_the_terminal();
    print_the_numbers_in_reverse_order();
}
That is OK, but it's missing something rather important. The "print the numbers" routine needs to know what numbers to print. So you need to store the numbers somehow. Suppose you store them in an array. You also need to know how many numbers you read in.

The "read" routine needs to output the data so you can pass it to the "print" routine.

So you can add a that to the program:
Code:
int readnumbers{int* numbers);
void reverseprint(int* numbers, int howmany);

int main()
{
    int numbers[50];
    int howmany;

    howmany = readnumbers(numbers);
    reverseprint(numbers, howmany);
}
That is now real C, except you need to write the two functions.

For "read numbers" you could start with something like this. (I've jumped a step ahead, and decided you need a variable "howmany" - note, this is a local variable, it's not the same as the "howmany" in the main program!
Code:
int readnumbers{int* numbers)
{
   int howmany;
   prompt_for_how_many_numbers();
   read_how_many_numbers();
   prompt_to_enter_the_numbers();
   for i = 1 to howmany
   {
      read_the_next_number();
   }
   return howmany;
}
Most of the steps in "readnumbers" are also done by function calls, except they are things like printf() and scanf() which you don't have to write for yourself.

Then, do the same for "reverseprint"...

This might seem longwinded, but the important thing is the thought process, not the relative trivia of how much detail you actually write down.
 
  • #7
Note that AlephZero's code is not quite C just yet, and in particular the top line of the for loop.

Leroy, I hope that you are starting to get the idea, from AlephZero's and DavidSnider's comments in this thread, and jtbell's comments in the other thread you have going, of how important it is to develop your program from the top down, starting with an algorithm. It is crucial to have a clear understanding, in broad strokes, of what your program needs to do. The details of how the functions accomplish the details can and should come later.
 

1. What is a function in programming?

A function in programming is a block of code that performs a specific task and can be called upon multiple times throughout the program. It allows for code reusability and makes programs more organized and efficient.

2. Why should I put a part of my program into a function?

By putting a part of your program into a function, you can reduce code duplication and make your program more modular. This also makes it easier to debug and maintain your code in the future.

3. How do I create a function in my program?

To create a function in your program, you need to define it using the keyword "def" followed by the function name and parentheses. Inside the parentheses, you can specify any parameters that the function will take. Then, write the code for the function inside curly braces ({}) and use the "return" keyword to specify the output of the function.

4. How do I call a function in my program?

To call a function in your program, simply write the function name followed by parentheses and any arguments that need to be passed into the function. For example, if your function is named "calculate_sum" and takes two numbers as parameters, you would write "calculate_sum(5, 10)" to call the function.

5. Can a function return more than one value?

Yes, a function can return more than one value by using the "return" keyword followed by a comma-separated list of values. These values will be returned as a tuple, which can then be unpacked and assigned to variables when the function is called.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
933
  • Programming and Computer Science
Replies
4
Views
901
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
996
Back
Top