0% found this document useful (0 votes)
8 views7 pages

Functions in C

Uploaded by

jhasandhaniram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Functions in C

Uploaded by

jhasandhaniram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FUNCTIONS IN C

What are Functions?

A function is a block of code that performs a specific task. It has a name and it is reusable i.e. it
can be executed from as many different parts in a program as required. It also optionally returns
a value to the calling program.

Properties of function in a C program:

● Every function has a unique name. This name is used to call the function from the
“main()” function. A function can also be called from within another function.

● A function is independent and it can perform its task without intervention from or
interfering with other parts of the program.

● A function performs a specific task. Such as adding two or more integers, sorting an array
into numerical order, or calculating a cube root etc.

● A function may return a value to the calling program. This is optional and depends upon
the task your function is going to accomplish. Suppose you want to just show a few lines
through a function then it is not necessary to return a value. But if you are calculating the
area of a rectangle and if you wanted to use the result somewhere in the program then
you have to send back (return) value to the calling function.

Some built-in functions in C:

❖ printf, scanf, system( “cls”), pow etc.

Function Definition in C

<return type> function_name (parameter-list) /* known as function header */


{
Declarations;

Statements;
}
❖ If the function does not receive any parameters, the parameter list is “empty”

❖ A type must be given for each parameter in the list. If no type is given, then type int is
assumed

Function Prototypes:

A function prototype tells the compiler the type of data returned by the function, the number of
parameters the function expects to receive, the types of the parameters and the order in which the
function expects to get those parameters. Function prototypes are used by a to compiler to
validate function calls.

Actual Parameters and Formal Parameters

Parameters/Arguments: define information that is passed to a function when it is called.

● Actual parameters are parameters that appear in function calls.


● Formal parameters are parameters that appear in function declarations.

An example of a function.

int sum (int x, int y)


{
int result; /* a local variable */

result = x + y;
return (result);
}

❖ Variables declared within the function can only be used within that function. These
variables are known as local variables and are local to the function they are declared in.

❖ Keyword <return> is used to return the result of the function back to the calling program
In the function main in the example above, fred, frank, and franny are all actual parameters when
used to call the function calculate_bill.

The variables in calculate_bill (diner1, diner2 and diner3, respectively) are all formal parameters
because they appear in a function definition.

Although formal parameters are always variables (which does not mean that they are always
variable parameters), actual parameters do not have to be variables. You can use numbers,
expressions, or even function calls as actual parameters.

Here are some examples of valid actual parameters in the function call to calculate_bill:

bill = calculate_bill (25, 32, 27);

bill = calculate_bill (50+60, 25*2, 100-75);


bill = calculate_bill (fred, franny, (int) sqrt(25));

There are many advantages in using functions in a program, they are:

1. It makes program development manageable by facilitating the top down modular


approach programming. In this style of programming, the problem can be broken down
into smaller blocks that are easier to manage.

2. The length of the source program can be reduced by using functions at appropriate
places.

3. It is easy to debug and correct programs. It becomes easy to locate and separate a faulty
function for further study.

4. It aids software reusability. They can be used as building blocks to create other programs.
Functions can prevent programmers from rewriting the same block of codes which are
going to be used at two or more locations in a program. This is especially useful if the
code involved is long or complicated. The function can be used by many other functions
or programs.

TYPES OF FUNCTIONS:

A function may belong to any one of the following categories:

1. Functions with no arguments and no return values.

2. Functions with arguments and no return values.

3. Functions with arguments and return values.

4. Functions that return multiple values.


void maximum( int arr[], int size )

{ int max;

Example:
#include <stdio.h>
void main()
{
int calculate_bill (int, int, int); /* function prototype */
int bill;
int fred = 25;
int frank = 32;
int franny = 27;
bill = calculate_bill (fred, frank, franny); /* Actual parameters */
printf("The total bill comes to $%d.00.\n", bill);
}
int calculate_bill (int diner1, int diner2, int diner3) /* function definition
with formal parameters*/
{
int total;
total = diner1 + diner2 + diner3;
return total;
}
int i;

max = 0;

for ( i = 0; i <= size; i = i + 1 )

if ( arr[i] > max THEN

max = arr[i]

printf ( “The maximum number if %d”, max );

You might also like