Functions (a)
In This Section:
Types of Functions, Built-in Functions, User-Defined Functions
Types of Functions
There are several different types of functions in C. So far, we have used one type of function: built-in C functions, like printf( ) and scanf( ). These functions are part of the C programming language.
Another type is called a user-defined function. This is a function which the programmer creates and uses in a C program. We will learn how to create these functions in this part of the tutorial.
We can also distinguish between functions which return values and those which don't -- like void main( ). Technically speaking, all functions return values, but in C we can choose to eliminate this process by using void. So far, we have not used our main( ) function to return any values, so we voided it and let the body functions, like printf( ) return values.
A function which return values produces some result -- like printing text on the screen, or scanning a variable, etc. Let's examine the different types of functions, so we can understand them better.
Built-in Functions
C has many built-in functions that you can use in your programs. So far we have learned 15 built-in functions:
|
main( )
printf( )
scanf( )
|
gets( )
puts( )
strcpy( )
|
strlen( )
strcmp( )
stricmp( )
|
strcat( )
strstr( )
isalpha( )
|
isdigit( )
isupper( )
islower( )
|
If you can't remember how to use each of these functions, go back and review the previous sections of this tutorial.
User-Defined Functions
If you have a special set of instructions that aren't in a built-in function, you can create a user-defined function. Here are the steps:
- give your function a name that isn't already used in C (by built-in functions, types of variables, keywords, etc.)
- create a function header, which contains three things:
a. the type of variable (int, char, double, etc.) that the function will produce (return)
b. the name of the function, which can be one or more words (but put underscores _ or CapitalLetters connecting these words, because no spaces are allowed)
c. the parameters of the function, which are the names and types of variables inside your function
- create a function body, which contains the operations to be completed when you call the function to run
Function Header
Here is an example of a function header:
int Square(int num)
This function expects an integer variable, so we begin with int.
Then we type a space followed by the name of the function, Square.
Next, we put the parameters in parentheses: (int num).
Function Body
Now we can add variables and functions to create a function body:
{
return (num * num);
}
This function calculates the square of num, which is num * num. This function returns the value of num * num.
Location and Call
To use this function, we must write it before void main( ) or after void main( ). We don't want to write it inside void main( ), since Square( ) is a function separate from main( ).
#include<stdio.h>
int Square(int num)
{
return (num * num);
}
Inside void main( ), we must call the function Square( ) to run. This means triggering the function to activate. This involves three things:
- a variable equal to the value returned from the function
- the name of the function
- the variable passed to the function
Here is an example:
void main( )
{
int num;
int total;
total = Square(num);
}
The Square Function
|
#include<stdio.h>
int Square(int num)
{
return (num * num);
}
void main( )
{
int num;
int total;
printf("Type a number: ");
scanf("%d", &num);
total = Square(num);
printf("Your number squared is %d.", total);
}
|
Using main( ) Plus Two More Functions
You can create more than one function in a C program. Each function will need its own return statement and its own variable to store the returned value. Here is an example:
|
#include<stdio.h>
int Thirteen(int born)
{
return (born + 13);
}
int Nineteen(int born)
{
return (born + 19);
}
void main( )
{
int born;
int teen1;
int teen2;
printf("What year were you born? ");
scanf("%d", &born);
teen1 = Thirteen(born);
teen2 = Nineteen(born);
printf("You were a teenager from %d to %d.", teen1, teen2);
}
|
Let's analyze this program step-by-step:
|
#include<stdio.h>
int Thirteen(int born)
{
return (born + 13);
}
|
This is the first function: Thirteen, which uses the passed variable born. This function returns a value of born + 13.
|
int Nineteen(int born)
{
return (born + 19);
}
|
This is the second function: Nineteen, which uses the passed variable born. This function returns a value of born + 19.
|
void main( )
{
int born;
int teen1;
int teen2;
|
In the main( ) function, declare three variables:
- the passed variable
- the variable to store the returned value from the first function
- the variable to store the returned value from the second function
|
printf("What year were you born? ");
scanf("%d", &born);
|
Ask the viewer to enter a value, then scanf it.
|
teen1 = Thirteen(born);
teen2 = Nineteen(born);
|
Pass the variable to each function, and store the returned value in our two variables.
|
printf("You were a teenager from %d to %d.", teen1, teen2);
}
|
Print the result.
|