Adding a main function

This is a discussion on Adding a main function within the C Programming forums, part of the General Programming Boards category; Okai here is the Code so far, It's not bad But the teacher told me to add a Main function ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    20

    Adding a main function

    Okai here is the Code so far, It's not bad But the teacher told me to add a Main function that calls my sum function. WTF? wouldn't the compiler go crazy if I change the 'main' function because its the MAIN! function. And what does he mine by a function that calls the other. Here is the program it works, but I need someone to just organize it with a function that calls this sum function. So this is simple basic C-free program so no complex things please
    Code:
    
    #include <stdio.h>
    
    
    int sum(int x, int y);
    
    
    
    
    int
    main(void)
    {
    	int x,y;
    	x=7;y=2;
    	
    	printf("    x   y  result\n\n");
    	
    	printf("%4d%4d%4d\n",x,y,sum(x,y));
    	
    	printf("%4d%4d%4d\n",x,y,sum(y,x));
    	
    	printf("%4d%4d%4d\n",sum(sum(y,x),y),y,sum(x,y));
    	
    	printf("%4d%4d%4d\n",(sum(sum(y,x),sum(y,x))),y,sum(x,y));
    	
    	printf("%4d%4d%4d\n",(sum(sum(y,x),sum(y,x))),sum(y,y),sum(x,y));
    	
    	return(0);
    }
    
    
    int sum(int x,int y)
    {
    	int result;
    	result=x+y;
    	
    	
    	return(result);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,210
    That main() function does call the sum() function.

    I'm guessing your teacher wants to see things that are more readable and clear, rather than aspiring to do multiple things in single statements.
    Code:
       some_variable = sum(x,y);
         /*  lots of other similar but distinct statements */
    and then separate
    Code:
        printf("%4d%4d%4d\n", x,y, some_variable);
         /*  other similar statements to print out other variables */
    Simply replace "some_variable" with variable names that are meaningful to your program.

    Bear in mind that, if you write multiple effects into single statements, the code is harder to read, header to get right, and harder for someone else (such as a teacher marking your homework) to understand.

    Teachers are often unwilling to put effort into understanding code that is unnecessarily hard to read, such as yours. View that as a preparation for real-life programming - I once sacked a programmer who insisted on writing code that was hard to read.
    Last edited by grumpy; 01-07-2013 at 01:43 AM.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    This program when run is supposed to make these answers... So if I do what your saying it would give me these answers?
    x y z
    7 2 9
    7 2 9
    11 2 9
    18 2 9
    18 4 9

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,210
    Sure, if you do it right. Not if you do it wrong, obviously.

    My answer is generic. You need to flesh things out with some specifics - based on whatever is required based on the homework question you have been asked.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    It's no homework. we have this book here in UNI that we can do these exercises in preparation for our exams next week. And I'm training to write programs so that I won't screw up in the exam. So I want my program to exactly match the questions in the book and actually be prefect flawless ones

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by Moody Barrawi View Post
    This program when run is supposed to make these answers...
    x y z
    7 2 9
    7 2 9
    11 2 9
    18 2 9
    18 4 9
    If that's true, why not do

    printf("%d %d %d\n", 7, 2, sum(7,2));

    You can place similar statements to get the other results. And they are all called from main, fulfilling the requirements. It's not necessary to nest the function calls as in sum(sum(sum(...

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    OOh I actually get what you mean... But can I like do a *x in the function to change the value so it would change in the main program so I don't have to change it again and again over and over. So basically I can change it from there and like this.. So in the end how would it look like>?

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by Moody Barrawi View Post
    But can I like do a *x in the function to change the value so it would change in the main program so I don't have to change it again and again over and over.
    The &x while calling the function introduces a pointer, which can be useful sometimes, but if the functions only returns a single value you probably don't need it.

    If you want an example, suppose you want to find both the sum and the product of two numbers a and b using a single function. You might write the following function:

    Code:
    void find_sum_prod(int *sum, int *prod, int a, int b);
    This function should calculate a+b and place it into the integer that `sum' points to. Then it should calculate a*b and place it into the integer that `prod' points to.

    Functions that accept pointers generally follow this principle. But in your sum(a,b) example it makes more sense to return the sum using the return value, not to return it via a pointer.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    I see thanks I will try that now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  2. Replies: 2
    Last Post: 09-19-2012, 11:32 AM
  3. Replies: 35
    Last Post: 12-01-2011, 07:31 PM
  4. Replies: 3
    Last Post: 06-01-2011, 03:08 AM
  5. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 11:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21