You are on page 1of 2

C Language

Functions is a small sub-program that performs a particular task and supports the concept of modular
programming design techniques.
In modular programming, the various tasks that your overall program must accomplish are assigned to
individual functions and the main program basically calls these functions in a certain order.

TYPES OF C FUNCTIONS:
1. Library function or pre-defined functions, are the built-in function in C Programming system.
Example: main(), printf(), scanf()
2. User-defined function functions that are created or defined by the user.

REASONS OF USE:
Dont have to repeat the same block of code many times in your code. Make that code block a
function and call it when needed.
Function portability: useful functions can be used in a number of programs.
Supports the top-down technique for devising a program algorithm. Make an outline and hierarchy
of the steps needed to solve your problem and create a function for each step.
Easy to debug. Get one function working well then move on to the others.
Easy to modify and expand. Just add more functions to extend program capability.
For a large programming project, you will code only a small fraction of the program.
Make a program self-documenting and readable.
Example of user-defined function:
Create a C Program to add 2 integers. Make a function call to add integers and display sum in
main() function.

#include <stdio.h>
#include <conio.h>
int add(int a, int b); /* Function prototype (declaration) */

void main()
{
int num1, num2, sum;
clrscr();
printf(Enter 2 numbers to add: \n);
scanf(%d, &num1, &num2);
sum = add(num1, num2); /* Function call */
printf(Sum = %d, sum);
getch();
}

/* Start of user-defined function */


int add(int a, int b) /* Function header (Argument/Parameters) (Argument List/Parameter List) */
{
/* Start of function definition */
int add;
add = a + b; /* Return a + b; */
return add; /* Return statement of function call */
}
/* End of user-defined function */
ORDER TO USE FUNCTIONS:
1. Define the function.
2. Function declaration or function prototype.
3. Use the function in the main code.

FUNCTION DEFINITIONS HAVE THE FF. SYNTAX:


<return_type> <function_name> (<data_type> <variable_name> list)
{
local_declarations;
function_statements;
}
Return type void, int, float, double, char.

TWO MAJOR COMPONENTS OF FUNCTION DEFINITION:


1. Function header is the first line of function definition. When a function is called, control of the
program is transferred to function header.
o the return_type in the function header tells the type of the value returned by the function.
o the data_type, variable_name list tells what arguments the function needs when it is
called (and what their types are)
2. Function body
o the local_declarations in the function body are local constants and variables that the
function needs for its calculations/process.
o the function_statements are composed of sequence of instructions needed to run the
function.
3. Function prototype (Declaration) every function in C Programming should be declared before
they are used. These type of declaration are also called function prototype. Function prototype gives
compiler information about function name, type of arguments to be passed and return type.
4. Function call control of the program cannot be transferred to user defined function unless it is
called invoked.

CONSIDERATION WHEN USING FUNCTIONS:


The number of arguments in the function call must match the number of arguments in the function
definition.
The type of the arguments in the function call must match the type of the arguments in the function
definition.
The actual arguments in the function call are matched up in order with the dummy arguments in
the function definition.

A function returns a value to the calling program with the use of the keyword return, followed by a data
variable or xonstant value. The return statement can even contain an expression.
Some examples:
return 3;
return n;
return ++a;
return (a * b);

You might also like