Jump to content

Functions: Difference between revisions

From IdeaWazaWiki
wikademia>Remi0o
m added cat
wikademia>MichaelFrey
mNo edit summary
Line 1: Line 1:
This example is on functions.  With Functions you  need to state the return type of the function.  Either int or void.  Int is value used for returning something to a call, mainly used for returning integers such as the add function in the following program.  Void is used for functions that dont return a value or you dont want to return a value.  You must use int or void in every function you make.  Take a look at the following program:
This example is on functions.  With Functions you  need to state the return type of the function.  Either int or void.  Int is value used for returning something to a call, mainly used for returning integers such as the add function in the following program.  Void is used for functions that don't return a value or you don't want to return a value.  You must use int or void in every function you make.  Take a look at the following program:


<pre>
<source lang="c">
#include <stdio.h>
#include <stdio.h>


Line 24: Line 24:
     return 0;
     return 0;
}
}
</pre>
</source>


You could have done this(it really doesn't make a difference):
You could have done this(it really doesn't make a difference):
<pre>
<source lang="c">
#include <stdio.h> //standar C header
#include <stdio.h> //standard C header


int cfile(char *file)
int cfile(char *file)
Line 51: Line 51:
     return 0;
     return 0;
}
}
</pre>
</source>


[[Category:Programming]]
[[Category:Programming]]

Revision as of 06:41, 17 May 2007

This example is on functions. With Functions you need to state the return type of the function. Either int or void. Int is value used for returning something to a call, mainly used for returning integers such as the add function in the following program. Void is used for functions that don't return a value or you don't want to return a value. You must use int or void in every function you make. Take a look at the following program:

<source lang="c">

  1. include <stdio.h>

void cfile(char *file) {

    FILE *fp = fopen(file, "w");
    fprintf(fp, "some text");
    fclose(fp);

}

int add(int x, int y) {

    int r;
    r = x + y;
    return(r);

}

int main() {

    cfile("example.txt");
    add(1,1);
    return 0;

} </source>

You could have done this(it really doesn't make a difference): <source lang="c">

  1. include <stdio.h> //standard C header

int cfile(char *file) {

    FILE *fp = fopen(file, "w");
    fprintf(fp, "example");
    fclose(fp);
    return 0;

}

int add(int x, int y) //this must be int it's returning something to a function call {

    int r;
    r = x + y;
    return(r);

}

int main() {

    cfile("example.txt");
    add(1,1);
    return 0;

} </source>