Thread: functions

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    4

    functions

    sorry for a basic question but functions are now confusing me.

    I want to know how i decide what value is returned, if any.

    when do i use: void funct_1(int x, int y)
    or

    int funct_1(int x, int y).

    I need this in complete laymens terms if anyone can be of assistance. Thank you.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ok please forgiwe the typing as I lost a few keys on the keyboard! Now take the funktion you wrote--if you wish to return "y" your funktion will be: int funk_1(int y){//your kode// return y;} So if you want to return a khar or string: khar funk_2(khar t){//kode// return t;} So on and so on---Now of kourse you kan return a pointer like so: khar* funk_3(khar* j){//kode//return *j;} ---And you kan also return a strukt -an array of ints and so on but I unsure of that syntaks-- Anyway I got to go fiks ny konputer!!!!!!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    void func_name()
    void as a return type should be used when everything you plan to do with the data in the function is to be used only in that function. Should you choose to give the function parameters, such as adding two numbers:
    Code:
    void add_two(int x, int y)
    then you plan to add your two numbers and then if you want to print them you would do so inside that function.

    Code:
    int func_name()
    A data type besides void tells the function that it is to send an item of data back to the part of your program that called the function. Say you call this function with a return type int from main():
    Code:
    int add_two(int x, int y){
      int sum;
      //do stuff
      return sum;
    }
    using return sum; you told the function to send sum back to main where you can assign it to a variable local to main and then print it out or modify it. sum itself does not change until you call the function again, you just give the value of sum to another variable so that you can use it in main.

    You can send any number of data types back to the function that called your function. int, char, float, double, pointers, etc...

    Here's the full program to add two numbers using a return type of int:
    Code:
    #include <stdio.h>
    int add_two(int x, int y);
    
    int main(){
      int one, two, three;
      printf("Enter two numbers: ");
      scanf("%d%d", &one, &two);
      three = add_two(one, two);
      three++;
      printf("The sum of your two numbers is %d", three);
    
      return 0;
    }
    int add_two(int x, int y){
      int sum;
      sum = x + y;
     
      return sum;
    }
    The output of this program is the sum of the two numbers the user entered in plus one.

    Hope that helps
    -Prelude

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    4

    functions

    Prelude,

    Thanks for that, that is the clearest reply i have ever had to that question.

    Thanks for taking the time to help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM