Writing your own functions

This is a discussion on Writing your own functions within the C Programming forums, part of the General Programming Boards category; Alright everyone check this out I'm a first year programming student and were in the chapter where we are required ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    Writing your own functions

    Alright everyone check this out I'm a first year programming student and were in the chapter where we are required to write our own functions. Well the book we have is kind enough to give us functions with one variable
    example.#include <stdio.h>


    double tempvert(double);

    int main()

    {
    int count;
    double fahren;
    for (count = 1; count <= 4; ++count)
    {
    printf("enter a temparature\n");
    scanf("%lf", &fahren);
    printf("the celsius equivilent is %6.2lf\n", tempvert(fahren) );
    }
    return 0;
    }
    double tempvert(double fahren)
    {
    return ( (5.0 / 9.0 * (fahren - 32) ));
    }

    but unfortunatley they neglect to give us any examples of writing your own function which would contain two variables. So basically I've been left out to dry by the book. I tried working it out logically and it just isn't making any sense does anyone out there have an example using two variable prewritten functions

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>but unfortunatley they neglect to give us any examples of writing your own function which would contain two variables

    you mean a function that will receive as parameter two variables? well, it's easy... look at the prototype, this function will receive 2 integers, and return their sum.

    Code:
    int add (int a, int b) {
        return (a+b);
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    exactly

    Thats exactly what I'm talkin about the only problem is now I just need to see one simple program that adds or subtracts two numbers utilizing a user written function and I'm homefree

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Well not caring about negative numbers and such, what about

    Code:
    #include <stdio.h>
    
    int getSum(int a, int b)
    {
       return a+b;
    }
    
    int getDif(int c, int d)
    {
       return c-d;
    }
    
    int main()
    {
       printf("3+5 = %d\n", getSum(3,5));
       printf("3-5 = %d\n", getDif(3,5));
       
       return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Originally posted by Vber
    >>but unfortunatley they neglect to give us any examples of writing your own function which would contain two variables

    you mean a function that will receive as parameter two variables? well, it's easy... look at the prototype, this function will receive 2 integers, and return their sum.

    Code:
    int add (int a, int b) {
        return (a+b);
    }
    Code:
    #include <stdio.h>
    
    int add (int a, int b) {
        return (a+b);
    }
    
    int main(void) {
        printf("The sum of 4 and 5 is %d\n",4,5,add(4,5));
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    I've almost got it

    Thats exactly what I'm talking about, how about one with two scanf statement

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Homework is fun.

    Code:
    #include <stdio.h>
    
    int getSum(int a, int b)
    {
       return a+b;
    }
    
    int getDif(int c, int d)
    {
       return c-d;
    }
    
    int main()
    {
       int n1, n2;
    
       printf("Enter num1: ");
       scanf("%d", &n1);
    
       printf("Enter num2: ");
       scanf("%d", &n2);
    
       printf("%d + %d = %d\n", n1, n2, getSum(n1, n2));
       printf("%d - %d = %d\n", n1, n2, getDif(n1, n2));
       
       return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Re: I've almost got it

    Originally posted by drdodirty2002
    Thats exactly what I'm talking about, how about one with two scanf statement
    how about you do your own-homework? bahh doing homework for other people it's not fun.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    I wish

    I wish this answered my homework but unfortunatley the question that I have for my homework is alot more complex than that

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    you asked something, we answered, post your code and the question, and we'll try to help you to complete the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to put local auxiliary functions?
    By draugr in forum C++ Programming
    Replies: 10
    Last Post: 03-17-2009, 08:46 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 06:05 PM
  3. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  4. newb question about interpreting and writing functions
    By crazychile in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 07:51 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

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