Programming language
Think you need to tell a computer to count the number of green beans from the mix of green and black, the instruction may like this:
Look through each of the beans, if it's a green one, count one more and put it to another side. Continue until there isn't any bean.
That's quite natural for you, but it is not understandable for computers. The computers, they are quite dumb, they has the language capbility no more than a four-year-old-child. We, as coder programmers have to translate our language into the language which the computer can understand - named byte code. However, each CPU has its own set of instructions it can understand, so for each type of CPU there are diffrent byte codes. Think of this as many different childrent speak different language, and you have to tell all of them what to do. People are smart, so they come up with a solution: They write a program that do the translation automatically, and they also create a medium language for this program - programming language - which stand between speaking language and computer language. In C, the above instruction can be rewritten like this:
/* beans.c */
#inc <stdio.h>
int main() {
int i, count;
int beans[10];
beans[0] = 1;
beans[1] = 0;
beans[2] = 0;
beans[3] = 1;
beans[4] = 1;
beans[5] = 1;
beans[6] = 1;
beans[7] = 1;
beans[8] = 0;
beans[9] = 0;
for (i = 0; i <= 10; i++) {
if (IsGreenBean(beans[i])) {
count++;
}
}
}
While this is not a runnable C program, it looks quite good. Let's dive into it.
C Functions
There is similarities between this new example and hello.c
. You have already known about the first
line, the semicolons and the curly brackets. Now we will discuss the building block of a C program:
functions. You have seen one, it is the main()
function:
int main() {
...
return 0;
}
A function is group of statements to acomplish one task, with or without some given condition. There
is another funtion you have known: it is the printf()
function. When use printf("Some text");
,
you run the function, or more technically: called it. Functions can be classified into two kinds, but
I tent to categorized them as three:
- The void functions: this is a commond kind of function. In other programming language like pascal, void functions are called procedure, because they does not act like math functions, they can take in some number but does not return anything.
- Normal functions: in contrast with void functions, normal functions act exactly like math functions, they take in variables and return one value through their name.
- The
main()
function: this is a must have function in every C program. When you execute the program, themain()
function is called. It can be a void function, but in modern programming, it is often declared as a normal function like I do above. It returns a integer, 0 if everything is OK and other if not. This is a good practice since we can read the state of a function through it returns value.
Function declaration
Go back and reobserve the given example of function declaration above. The first line tells the compiler that we will make a function named main, it takes no argument while returns an integer. Any function declaration can go like this:
return_type function_name(taken_arguments) {}
If you look at the stdio.h file you can file the printf()
function is declared with something like
this:
int printf (__const char *__restrict __format, ...);
Because the main()
function doesn't require any argument, so we leave taken argument field blank.
However, printf()
require some argument, so it is declared as above.
Note: C is case-sensitive, so print
is not equivalent to Printf
or PRINTF
.
Function call
We created a powerful function, then whenever we want to use it, we have to call it. Also, you have already know this.
printf("Hello, world!\n");
That's it! FunctionName(arguments). If the function has return value (it is a normal function), we can call the function and assign the returned value to a variable like this:
a = printf("Hello, world!\n");
The value of a
is 0.
Exercises
Programming exercises:
- Examine function
IsGreenBean()
in the complete code, insert aprintf
statement inside the function body to ensure that the function is called ten times. - Modify
hello.c
, create aSayHello()
function that print the welcome message. Call this function 5 times in themain()
function so it print out 5 welcome messages when run. - Create a
Add()
function that takes two integers as argument, and return an interger value equals to the sum of the two arguments. - Write a program using the
Add()
function you have created. Call it a few time to make sure it work. Ex:printf("The sum of 3 and 5 is: %d\n", Add(3, 5));
Reading exercise: Read about the printf()
function, find out the meaning of %d
and \n
.
Source code: beans.c