calling a function??

This is a discussion on calling a function?? within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm a rookie when it comes to C but I'm learning. I was wondering if someone can give ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    calling a function??

    Hi all,
    I'm a rookie when it comes to C but I'm learning. I was wondering if someone can give me advice on my code? I'm trying to write a function that receives from the calling program the length and width of a room in decimal feet. The function should then caculate and return the number of square yards in the room.

    I would like my main program to prompt for the dimensions of the room in decimal feet and the cost of a yard of carpet. Then, I would like to display both the number of yards needed and the total cost of the carpet.

    I think I have my code in my function mixed up with the code in my main program. I didn't totally finish because I'm at this roadblock. Please, this is probably basic to some, but I'm just starting out. Thanks in advance for any advice. Here is my code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    double room(double length, width, yds)
    {
    yds=9/(length*width);
    }
    
    return yds;
    
    int main()
    {
    double length, width, cost, total;
    
    cout<<"Enter the width of the room:";
    cin>>width;
    cout<<"Enter the length of the room:";
    cin>>length;
    cout<<"Enter the cost of a yard of carpet:";
    cin>>cost;
    
    total=room*cost;

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    double room(double length, double width, double yds)
    {
    return yds=9/(length*width);
    }
    Code:
    total = room(length, width, yds) * cost;
    Last edited by The Brain; 04-15-2005 at 08:16 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I think you need to read up on how to use functions and how to properly syntax them.

    I have given you a code for a simple function which calculates the square of a given number.

    You should be able to read through and adapt it to meet the needs of your specific program.



    Code:
    #include <iostream>
    
    //function declaration
    //always before int main()
    //semi colon needed.
    //If more than one variable is used
    //separate them with a comma
    int square(float);
    
    using namespace std;
    
    int main()
    {
        float number;
        cout<<"Enter a number:";
        cin>>number;
        
        // call the function 'square' passing in the variable 'number'
        // into it. Here you don't need to define its type
        //semicolon needed
        square(number);
        
        cout<<"Press any key to exit"<<endl;
        char pause;
        cin>>pause;
        
    }
    
    
    //function definition
    //here you have to declare what type the variable is
    //Here the semi colon is ommitted.
    int square(float number)
    {
        
        cout<<"The number squared was"<<number*number<<endl;
        
    }
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 06:32 PM

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