0% found this document useful (0 votes)
12 views

CHAP 11 (Functions)

Uploaded by

kayaniwaheed721
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CHAP 11 (Functions)

Uploaded by

kayaniwaheed721
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER #11

FUNCTIONS
Definition of functions:

A function is a block of instructions that together perform a specific task. Every C program has at
least one function, main ( ).you can also write your own functions and use them just as you use
functions in a C library.
hen your program starts, main ( ) is called automatically and it is always executed first, main ( )
might called other functions, some of which might called still others.

Advantages / importance of functions:


The following are some advantages of using functions.
1. The complexity of the entire program can be divided into simple subtasks and function
subprograms can be written for each task.
2. Functions help us to avoid unnecessary repetition of code. Using functions a single section
of code can be used many times in the same program.
3. Functions can have inputs and outputs can process information.
4. The function or subprograms are short, easier to write, understand and debug.
5. A function can be shared by other programs by compiling it separately and loading them
together.
6. Functions are mostly independent of each other, just as one program is independent of other
program. Most of the other activities in a function are hidden from the rest of the program.
7. In turbo C++, a function can call itself again and again. It is called recursiveness. Many
calculations can be done easily using recursive process such as calculation of factorial of a number
etc.
Classification of functions:
Functions are of two types. These are:
1. Built – in functions / library functions
2. User defined functions

Built in functions:
Many of the operations, like taking the square root of a number, sine value of a number
etc. will be frequently used by many programmers in their programs. Such operations are
Programmed and stored in Turbo C++ library, so that they can be called through any program in
the form of functions. These functions are called library functions or built-in functions.

Some mathematical built- in library functions:


User-defined functions:
A part from the library functions that are built in Turbo C++, users can also defined functions to
do a task a relevant to their programs. Such functions are called user defined functions. These
functions should be codified by the user, so that any call to the function can refer to it.

General form of functions:


The overall structure to the function looks almost exactly like that of the main ( ) function in a
program. That is because main ( ) is also a function. The only difference is that main is a special
name. use of the function in a program requires three things .
1. Function declaration, which is also called function prototype.
2. Function definition.
3. Function calling.

1. Function prototype:
The declaration of a function is called its prototype. It is a line written before the main ( ) function.
The prototype tells the compiler in advance about some characteristics of a function used in the
program.
The general structure of the function protype is :
Type function _ name ( type argument-1, type argument-2,..................... );
It has three main components. These are:
1. Data type of the function
2. Name of the function
3. Arguments

Data type of the function:


The data type of the function is a type of the returned value. If the function does not return a value
, the type is defined as void.

Name of the function:


The name of the function is any legal in identifier followed by the parenthesis without any spaces
in b/w.

Argument:
The argument come inside the parentheses , preceded by their type and separated by commas. If
the function does not use any argument, the word void is used inside the parenthesis.

2. Function definition:
The function definition is the function itself. The general structure of the function definition is :
Type function _ name (type argument – 1, type argument – 2 , . . .)
{
Body of function;
}
The function definition has two parts:
1. Function header
2. Body of function

Function header:
The function begins with a header which is exactly same as the function prototype except it must
not be terminated with semicolon (;). Header specifies the name of the function. It also describes
whether you are using a simple function or passing the arguments or returning the value from the
function.

Body of function:
It is the main body of the function. Al statements are written in this part of the program. The
statements in the body of the function are written to perform a specific task.

3. Function calling:
A user defined function is called from the main program simply by using its name, including the
parentheses which follow the name. The parentheses are necessary so the compiler knows you are
referring to a function and not a variable that you forgot to declare.
Function With Or Without Arguments:
It would not be useful if all functions were as independent of each other as main ( ). Most functions
depend on other functions to some extent. For example, it does not make sense to call the pow
(raise to the pow) function without specifying values for the base and exponent. Moreover, pow is
not useful unless it sends back the result of the computation.
Function can be used in the four variations:
1. function that does not need any arguments and also returns no value.
2. Function that does not need any argument but returns a value.
3. Function that needs one or more arguments but returns no value.
4. Function that needs one or more arguments and also returns a value.

Passing ArgumentsTo A Function:


You can pass an argument to your own functions. Any data you want to send to the function must
be in the parentheses. You can pass more than one argument, as long as the number of arguments
in the function call, and their data types, match those expected by the function. The list of variables
being passed to a function is called an argument list.
Passing Multiple Arguments To A Function:
You can pass as many items to the function as you need, of any type. The function area ( )
calculates the area of a floor. The length and width of the floor, and the floor’s number are input
in main ( ), then pass to the function in the call.
Area (length, width, fnum);

Here is the program:

#include<stdio.h>
#include<conio.h>

void area(float size, float wide, int num);


void main (void)
{
clrscr( );
float length, width;
int fnum;
printf (“\n enter the floor number:”);
scanf (“%d”, &fnum);
printf(“\n enter the length of the floor:”);
scanf(“%f”, &length);
printf (“\n enter the width of the floor:”);
scanf(“%f”, &width);
area ( length, width, fnum); // length, width and fnum are actual arguments
getch( );
}

Function That Return A Value:


Your awn functions return a value or void. As stated earlier that void is a signal to the compiler
that no value will be returned. To return a value from a function, write the keyword return ( )
followed by the value you want to return. The return ( ) statement has two purposes:
1. Executing it immediately and transferring control from the function back to the calling
program.
2. Whatever is inside the parentheses following is returned as a value to the calling program.
Using More Than One Function In A Program:
You can use as many functions as you want in a program, and any of the functions can
call any of the other function. All the functions are accessible to the all other the
function.

You might also like