Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Introduction to functions

Uploaded by

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

Introduction to functions

Uploaded by

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

1

Introduction to functions

Functions are important part of code in C. A C program cannot successfully link without the
function main(). A function is C is a named block that is called upon to perform a definite task.
It is provided data to perform the desired operation and it then returns a value to the statement
that called it. C program can be viewed as a collection of independent functions to perform a
logical task of some kind. Function can be called n number of times to perform a similar task,
without having to code for it again and again.

Advantages of functions

Here are some of the advantages of using functions.

1. Code re-usability: The code inside the function can be used N number of times without having
to rewrite the code again and again. The programmer simply needs to place a call to that function
and the compiler takes care of the rest. It reduces the effort of programmer.

2. Lesser Complexity: Large problems can be split into number of smaller and easy to
understand and code functions. The functions can then be called according to requirement.

3. Easy and fast Debugging: Debugging refers to finding logical errors in the program. Using
functions debugging becomes much easier and faster. Errors can be fixed with a lot of ease. It
can get very difficult to find bugs in a single function that is large in size.

4. Recursion: Complex computer science problems can be reduced to almost single line codes
using recursion concept. By recursion we mean when a function calls itself.

5. Improved readability: The program is divided into smaller low level functions reducing the
length of the source program. The functions can then be placed in multiple files, making the
source file easy to understand.

6. Function sharing: A function can be placed in a separate file and then it can be shared by
different program.

Disadvantage of functions
2

Beside all the advantages, there are disadvantages that are associated with the use of
functions. While it adds speed to coding, it slows down the speed of the program execution. The
program during execution has to jump to a function when a call is placed to it and return after
the function completes execution.

Function declaration

Just like any variable, a function should also be declared or prototyped before it can be used.
Compiler should be informed about the existence of a function, i.e. a function of such a name
exists. The function declaration gives the basic structural information about the function being
created to the compiler like, return type, name, type and number of arguments [1]. When a call
to any function is made, it looks to match it with the prototype of that function first. If it does not
find any prototype matching the function that is called, it produces an error. A function can be
declared or prototyped as follows:

Syntax,

return_type function_name (type1, type2, …, typen);

For example,

int add (int, int, int);

Function call

The program executions starts from the function main(). Rest of the functions cannot execute
on their own. For a function to be executed, a call to that function is required. When a call is
made to the function, compiler looks for the declaration or prototype of a function that matches
the prototype of function called. The program control then jumps to the function definition of the
called function. A called function then executes the piece of code written in its definition and
returns back to the statement that was used to call it. We can also pass certain information
to the function as arguments.

For example,

int a, b, sum;

sum = add (a, b); // function call


3

Function definition

Function definition is where we write the code. A function definition in C programming


language consists of a function header as the first line followed by the function body enclosed
in curly braces. Let us see the content of function header one by one

1. Return Type: A function may or may not return a value. In case a function returns a value,
the return_type is the type of value returned by the function. In case the function does not return
a value, the return_type can be specified as void.

2. Function Name: This is the actual name or identifier of the function. More than one function
can also have the same name. so one function is distinguished from the other on the bases of
parameter list.

3. Parameters: A function header consists of parameter list that is used to accept the values
passed during function call. When a function is called, a set of values may be passed to the
parameter. These values are referred to as actual parameters.
4. Function Body: This is where the actual code is written. The function body is block
enclosed using curly braces. The function body contains a collection of statements to perform
the specified task.

Return statement

A function may or may not return a value. The statement used for returning value from a function
is return statement. A function may not return a value, or at most a function can return only one
value. Still, a function can have more than one return statement in the code, but only one of it
can be executed on the basis of a condition. The concept of returning more than one value is
also discussed in this lesson.

To find sum of two numbers using functions

#include<conio.h>

#include<stdio.h>

//function prototype or declaration


4

int add (int, int);

void main()

int var1, var2, sum;

//accept input

printf ("Enter first number :- ");

scanf("%d",&var1);

printf ("Enter second number :- ");

scanf("%d",&var2);

//function call

sum = add (var1, var2);

//displaying the result

printf("Sum of %d and %d is :- %d", var1, var2, sum);

getch();

//function definition

int add (int num1, int num2)


{
//function body

int rval;

rval = num1 + num2;

// returning the sum


return rval;

To compute factorial using functions

#include<conio.h>

#include<stdio.h>
5

//function prototype or declaration

int factorial (int);

void main()
{

int var1, fact;

//accept input

printf ("Enter any number :- ");

scanf("%d",&var1);

//function call

fact = factorial(var1);

printf("\nFactorial of %d is :- %d", var1, fact);

getch();

//function definition

int factorial (int num1)

//function body

int i, fact=1;

for (i=1;i<=num1;i++)

{
fact = fact * i;

return fact;

Types and categories of functions


6

The very first program written in this book used functions, even though not created by us. In this
lesson we have also seen how we can create our own functions and how they get executed.
On the basis of these two differentiations, we have 2 types of functions in C.

✓ Library/In-built/standard functions.

✓ User-Defined functions.

Library functions

C language comes with a set of predefined library functions that can be used to directly
achieve some objective without having to code for that. C Library functions are inbuilt
functions in C programming. Function prototypes of these functions are written in their
respective header file. To use these functions, you must include their respective header files.
There are large numbers of library functions available in C programming to help the
programmer to write fast and effective code.
For example, if you want to find the length of the string, you can simply call a library function
called strlen() used to find the length of the string. For using strlen() function the programmer
must include its respective header file in the source program using the preprocessor
#include. The header file for strlen() function is string.h.

User-defined functions

Programmers can also create their own functions in C other than accessing the predefined library
functions. A user must define the following in order to create a user-defined function:

✓ Function prototype

✓ Function call

✓ Function definition

7.5.3 Categories of functions

The user-defined functions come in four categories.

1. Functions with no return type and no arguments.

2. Functions with no return type, but with arguments.

3. Functions with return type and no arguments.


7

4. Functions with return type and arguments.

We can create user defined functions of any of the four categories. Let us see the examples for
declaring and calling such functions.

1. Functions with no return type and no arguments.

// prototype

void display( void);

//function call

display();

2. Functions with no return type, but with arguments.

// prototype

void display( int, int);

//function call

display(a, b); // both of int type

3. Functions with return type and no arguments.

// prototype

int sum(void);

//function call

a = sum(); // a of int type

4. Functions with return type and arguments.

// prototype
8

int sum(int, int);

//function call

a = sum(b, c); // all three of int type

You might also like