There are several different types of functions in C. So far, we have used one type of function – the 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); } |
|
|||||||||||||||||||||||||||
Leave a comment