Small problem

This is a discussion on Small problem within the C Programming forums, part of the General Programming Boards category; Hello, I'm VERY new to C and started learning today. I have a slight problem, this is my objective: #5 ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Small problem

    Hello, I'm VERY new to C and started learning today. I have a slight problem, this is my objective:

    #5
    Write a C program that calls the C function you just wrote in exercise 4 to calculate the multiplication of 3 times 5 and then print out the return value from the function on the screen.
    This is what I wrote for the exercise 4 it was talking about:

    Code:
    //Function to multiply two ints and return result
    #include <stdio.h>
    //Multiply two ints
    int multiply( int x, int y)
    {
    	int result;
    	result = x * y;
    	return result;
    }
    
    int main()
    {
    	int answer;
    	
    	answer = multiply( 10, 5);
    	printf("Ten multiplied by five equals %d.\n", answer);
    	return 0;
    }
    Now, I didn't fully understand what #5 was stating, so can someone help me out? I got this from a tutorial I'm reading. Thanks.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by xViBeSz View Post
    Hello, I'm VERY new to C and started learning today. I have a slight problem, this is my objective:

    #5


    This is what I wrote for the exercise 4 it was talking about:

    Code:
    //Function to multiply two ints and return result
    #include <stdio.h>
    //Multiply two ints
    int multiply( int x, int y)
    {
    	int result;
    	result = x * y;
    	return result;
    }
    
    int main()
    {
    	int answer;
    	
    	answer = multiply( 10, 5);
    	printf("Ten multiplied by five equals %d.\n", answer);
    	return 0;
    }
    Now, I didn't fully understand what #5 was stating, so can someone help me out? I got this from a tutorial I'm reading. Thanks.
    what is #5 here. as far as ur code is concerned its fine.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    umm... sounds like you're finished


    Did you know:

    Code:
     
    int multiply( int x, int y)
    {
    	int result;
    	result = x * y;
    	return result;
    }
    is equivalent to

    Code:
    int multiply( int x, int y)
    {
    	return x * y;
    }
    and

    Code:
    int main()
    {
    	int answer;
    	
    	answer = multiply( 10, 5);
    	printf("Ten multiplied by five equals %d.\n", answer);
    	return 0;
    }
    is equivalent to

    Code:
    int main()
    {
    	printf("Ten multiplied by five equals %d.\n", multiply( 10, 5) );
    	return 0;
    }
    Last edited by ಠ_ಠ; 04-25-2009 at 11:43 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Ohhhhh, so this is the function:

    Code:
    int multiply( int x, int y)
    {
    	int result;
    	result = x * y;
    	return result;
    }
    And this is what calls it?:

    Code:
    int main()
    {
    	int answer;
    	
    	answer = multiply( 10, 5);
    	printf("Ten multiplied by five equals %d.\n", answer);
    	return 0;
    }
    And at above, that looks really weird to me, I'm going to go with what's int he tutorial first and then get shortcuts when I grasp it. Thanks though ^^

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Lil Help

    xVibe-

    You said

    Ohhhhh, so this is the function:


    Code:
    Code:
    int multiply( int x, int y)
    {
    	int result;
    	result = x * y;
    	return result;
    }
    And this is what calls it?:
    Code:
    int main()
    {
    	int answer;
    	
    	answer = multiply( 10, 5);
    	printf("Ten multiplied by five equals %d.\n", answer);
    	return 0;
    }
    Hopefully my formatting will work there. Anyway, You are correct. In this case, and in most cases that I have seen thus far, Main calls the function, or series of functions, and they execute, then return to main. Basically, line by line your code executes like this:

    1. int answer ; ---- This creates a variable named answer.

    2. answer=multiply(10,5); ---- This is the magic of the program. It begins the process of assigning the returned value of the expression to the right of the equals, to the variable at the left. It does this by "calling" the function named multiply, and giving it the values 10 and 5. These are temporarily assigned to the values of x and y you entered in your function header. Control of the program is handed to the multiply function in this step.

    3. The multiply function then makes a variable named result.

    4. The program assigns the value of x times y to the variable result.

    5. The program returns the result and control to the main function. It also assigns the returned value of result to the variable answer (this is the actual point at which that occurs (not step 2, although that step sets the series of events in motion that makes this happen)-it HAS to go through the multiply function first).

    6. Print something

    7. Return 0 at the end

    If there is anything you do not understand please post here or message me. I am not super at coding yet, but I am getting there. Try to avoid learning more than one language at a time, unless you have a photographic memory. That has been my downfall thus far.

    Darryl

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Thank you so much! It covered that in the chapter I was reading, but it was pretty.. vague. Thanks again.

    And yeah, I read about subtraction and addition are on a 'lower' thing and multiplication and division and this thing '%' are 'higher'.
    Last edited by xViBeSz; 04-26-2009 at 11:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 03:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM

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