For the final lesson we'll discuss funcions. On other programming language they are called procedures. There are some codes that might get used more than others. Functions makes it easier to get to the code. Functions separate certain functions into its own self contained mini-program. For example the Input Processing and Output function of the program can be separated into different functions. This leaves the main function smaller and less cluttered.
The syntax:
(No return value.)
void functionname()
{
statement 1;
statement 2;
}
(With return value.)
int functionname(int num)
{
statement1;
statement2;
return(num);
}
#include<stdio.h> #include<conio.h> int num1, num2; void input() { printf("Enter two numbers: "); scanf("%d %d", &num1, &num2);} int sum(int x, int y) {return(x+y);} void output() {printf("%d + %d = %d", num1, num2, sum(num1,num2));} void main() { clrscr(); input(); output(); getch(); } |
Notice how the main function is at the bottom and it calls the input and output function above. The output function calls the sum function. We'll that's the end of my tutorial, it's not comprehensive by all means but should help you get started.