I imagine your programs will need much more code than used so far, especially for a working game. However, one problem we face is a larger and unorganized main function. Also, some commands must be performed outside of main, so custom functions will help a lot. We will talk about output, input, and prototypes of functions.
output:
You may remember from the C basics post, http://multimediaprogram.blogspot.com/2012/05/c-basics.html,
a talk of how to declare functions. To reiterate, the syntax to create
a function which returns an integer and has no parameters is int functionName(), which is followed by a {, commands, and a closing }.
Like in main, a function which returns an integer needs a return statement. For main, return 0; indicates the end of main with a success (or the 0 code). Here is an example of a custom function which returns five:
#include <stdio.h>
int five() { return 5; } int wait_key() { getchar(); return 0; } int main() { int x = five(); printf("x = %d\n", x); return wait_key(); } | output in prompt: x = 5 |
The function five shown above returns 5. By changing the command return 5; to return 10; the function's output would become 10, but the function would still be named five. The wait_key function has also been brought back and updated; here, it returns the exit code to main, which then returns the output of wait_key.
Input:
As you have learned in math class, functions also have input. Think of y = f(x), y is assigned the output of the function f and x is the input of the function. To begin creating f, start with double f() { since real numbers are often used in mathematics. Now that input is required, the declaration of the function will change: double f (double x) {.
The added double x tells C that the function f requires one double as input. When calling the function f, you do not need to initialize x, since this has already been done in the declaration; all that is required is a value to pass to x. Here is an example of the function f declared and called:
There are two variables named x in this example: while C does not allow two variables of the same name, this only applies to variables of a given function. The function main has variables x and y while f uses x and output, so x can exist in two different functions.
Modifying x in f does not modify x in main, since they are two different variables. In fact, passing the value of main's x to f's x and vice versa is also acceptable. But what if we want one x that can be modified everywhere, without passing through functions? We would use global variables.
The following example is the same code above with a global variable called x:
Global variables are declared underneath the #include commands and before function declarations. Some changes to the example include no initializing x in main or passing its value to f (which now has no parameters). Since x is recognized by all functions, we cannot name a variable x in any functions.
Optionally, the function f could modify the global variable x and the effect could be seen in the main function. If the statement x = x + 1; were placed in f, main would recognize the change in x from 7 to 8 after the function f is called.
Prototypes:
All of the functions so far have been declared before the main function. This is because a function must be defined first before being used. However, one work-around is to create a function prototype which is the output type, name, and input of a function without any of the commands (which are added later).
The following example shows a few prototypes in action:
The function add is first defined in the form of a prototype which uses variable types without names, then with a regular function definition. The key difference between this example and previous ones is that function definitions for add and wait_key can be placed anywhere in relation to main.
Also, the function add accepts two parameters which are separated by a comma. You are free to define a function with any arbitrary number of parameters and commands, but there can only be one type of output (unless you count modifying global variables).
For more information about functions, the link http://www.tutorialspoint.com/ansi_c/c_using_functions.htm can provide some insight.
The next post will discuss conditions and logic.
#include <stdio.h>
/*function f requires one double
as input and returns one double*/
double f (double x) {double output; output = 2 * x + 1; return output; }
//no input required, returns int 0
int wait_key() {getchar(); return 0; }
//the main function
int main() {double x = 7, y; y = f(x); //call function f printf("f(%lg) = %lg\n", x, y); //call wait_key and return output return wait_key(); } | output in prompt: f(7) = 15 |
There are two variables named x in this example: while C does not allow two variables of the same name, this only applies to variables of a given function. The function main has variables x and y while f uses x and output, so x can exist in two different functions.
Modifying x in f does not modify x in main, since they are two different variables. In fact, passing the value of main's x to f's x and vice versa is also acceptable. But what if we want one x that can be modified everywhere, without passing through functions? We would use global variables.
The following example is the same code above with a global variable called x:
#include <stdio.h>
//x is now recognized by all functions double x; double f () { //no need to pass a value double output; output = 2 * x + 1; return output; } int wait_key() { getchar(); return 0; } int main() { double y; x = 7; //set value of x y = f(); printf("f(%lg) = %lg\n", x, y); return wait_key(); } | output in prompt: f(7) = 15 |
Global variables are declared underneath the #include commands and before function declarations. Some changes to the example include no initializing x in main or passing its value to f (which now has no parameters). Since x is recognized by all functions, we cannot name a variable x in any functions.
Optionally, the function f could modify the global variable x and the effect could be seen in the main function. If the statement x = x + 1; were placed in f, main would recognize the change in x from 7 to 8 after the function f is called.
Prototypes:
All of the functions so far have been declared before the main function. This is because a function must be defined first before being used. However, one work-around is to create a function prototype which is the output type, name, and input of a function without any of the commands (which are added later).
The following example shows a few prototypes in action:
#include <stdio.h>
//function prototypes
double add (double, double);int wait_key();
/*prototypes do not need names for the
input, just types. The names can be
provided in the definition*/
int main () { double x = 5, y = 8; double z = add(x,y); printf("%lg + %lg = %lg\n", x, y, z); return wait_key(); }
//now functions can be defined after main
double add(double a, double b) { return a + b; } int wait_key() { getchar(); return 0; } | output in prompt: 5 + 8 = 13 |
The function add is first defined in the form of a prototype which uses variable types without names, then with a regular function definition. The key difference between this example and previous ones is that function definitions for add and wait_key can be placed anywhere in relation to main.
Also, the function add accepts two parameters which are separated by a comma. You are free to define a function with any arbitrary number of parameters and commands, but there can only be one type of output (unless you count modifying global variables).
For more information about functions, the link http://www.tutorialspoint.com/ansi_c/c_using_functions.htm can provide some insight.
The next post will discuss conditions and logic.
No comments:
Post a Comment