Freshers Aptitude technical questions
Freshers Job Alert
Bookmark and Share

C Programming Tutorial

Learn C programming in an easy way.This C programming tutorial is completly free for your use.

How you can help us back is let us know about more tutorials and send us the tutorial with your small photo and name.We will publish tutorial in your name.

 

<< Tutorial Home


FUNCTIONS
A function in C can perform a particular task, and supports the concept of modular programming design techniques.
We have already been exposed to functions. The main body of a C program, identified by the keyword main, and enclosed by the left and right braces is a function. It is called by the operating system when the program is loaded, and when terminated, returns to the operating system.
Functions have a basic structure. Their format is

return_data_type function_name ( arguments, arguments )
data_type_declarations_of_arguments;
{
function_body
}
It is worth noting that a return_data_type is assumed to be type int unless otherwise specified, thus the programs we have seen so far imply that main() returns an integer to the operating system.
ANSI C varies slightly in the way that functions are declared. Its format is

return_data_type function_name (data_type variable_name, data_type variable_name, .. )
{
function_body
}
This permits type checking by utilizing function prototypes to inform the compiler of the type and number of parameters a function accepts. When calling a function, this information is used to perform type and parameter checking.
ANSI C also requires that the return_data_type for a function which does not return data must be type void. The default return_data_type is assumed to be integer unless otherwise specified, but must match that which the function declaration specifies.
A simple function is,

void print_message( void )
{
printf("This is a module called print_message.\n");
}
Note the function name is print_message. No arguments are accepted by the function, this is indicated by the keyword void in the accepted parameter section of the function declaration. The return_data_type is void, thus data is not returned by the function.
An ANSI C function prototype for print_message() is,

void print_message( void );
Function prototypes are listed at the beginning of the source file. Often, they might be placed in a users .h (header) file.

FUNCTIONS
Now lets incorporate this function into a program.

/* Program illustrating a simple function call */
#include <stdio.h>

void print_message( void ); /* ANSI C function prototype */

void print_message( void ) /* the function code */
{
printf("This is a module called print_message.\n");
}

main()
{
print_message();
}

Sample Program Output
This is a module called print_message.

To call a function, it is only necessary to write its name. The code associated with the function name is executed at that point in the program. When the function terminates, execution begins with the statement which follows the function name.
In the above program, execution begins at main(). The only statement inside the main body of the program is a call to the code of function print_message(). This code is executed, and when finished returns back to main().
As there is no further statements inside the main body, the program terminates by returning to the operating system.

In the following example, the function accepts a single data variable, but does not return any information.

/* Program to calculate a specific factorial number */
#include <stdio.h>

void calc_factorial( int ); /* ANSI function prototype */

void calc_factorial( int n )
{
int i, factorial_number = 1;

for( i = 1; i <= n; ++i )
factorial_number *= i;

printf("The factorial of %d is %d\n", n, factorial_number );
}

main()
{
int number = 0;

printf("Enter a number\n");
scanf("%d", &number );
calc_factorial( number );
}

Sample Program Output
Enter a number
3
The factorial of 3 is 6

Lets look at the function calc_factorial(). The declaration of the function

void calc_factorial( int n )
indicates there is no return data type and a single integer is accepted, known inside the body of the function as n. Next comes the declaration of the local variables,

int i, factorial_number = 0;
It is more correct in C to use,

auto int i, factorial_number = 0;
as the keyword auto designates to the compiler that the variables are local. The program works by accepting a variable from the keyboard which is then passed to the function. In other words, the variable number inside the main body is then copied to the variable n in the function, which then calculates the correct answer.

 

 

 

<< Tutorial Home