Functions in C Language



 Functions

A function is a self contained block of code that does some specific task. A function definition has a header and body.

Header

A header of a function is made up of

·         Return type: It is the data type of the value being returned by the function. If a function has no value to return. It has the return type as void.

·         Name : It is the name of the function by which you call it. The name of a function is its address.

·         Argument List : A function can have zero or more parameters known as formal parameters, separated by commas in case it is more than one in number. These formal parameters take values form the calling body so that it can perform the specific tasks on it. These arguments are enclosed within the pair of parenthesis. 

Body

Body of a function contains the actual logic to perform certain task. It may return a value of a particular type, if its return type is not defined as void. The body of a function should be enclosed within a pair of curly braces.

Syntax

type name(argument_list)

{

body

}

 Example : To write a program to get the largest of three values

#include<stdio.h>

float larger(float x, float y)

{

return x>y?x:y;

}

void main()

{

float a,b,c;

printf("Enter Three Numbers");

scanf("%f%f%f",&a,&b,&c);

printf(The Largest of %f ,%f and %f is %f",a,b,c,larger(a,larger(b,c)));

}

Properties of functions

1                     Functions perform well defined task and can be used anywhere in a program

2                     Reusability- A function is created once and can be used any number of times.

3                     Information hiding- Function contents are not available in the other part of the program where it is implemented.

4                     Extensibility - Functions are written to and called to get more functionality in the other program.

5                     Clarity - A function body is written once and saved in one part of the program and called in the other. which removes the extra cluttering from the program and increases the clarity in the codes.

6                     Maintainability - A function when used at several places can be modified without affecting the rest of the program and vice versa. Which makes the maintainability more efficient.

7                     Testing and debugging - Testing a program in bits and pieces is quite easy. So if you use multiple functions in your program then testing and debugging it individually would be quite easy to test and debug the errors.  

 

Function Prototypes

Before using a function in c, compiler need to know details about the function. So the syntax used to declare a function is called function prototype. The function prototype tells the compiler that -

·         The number of arguments being passed to the function

·         The type of each argument being passed to the function

·         Return type of the function

Syntax

return_type function_name(type of each argument)

int larger(int a, int b );

Here larger is the name of function with return type int. It has two integer arguments a and b. both arguments a and b here are optional. So you can also write this as -

int larger(int, int);

Types of Functions

1)            Library Functions - which are defined in the c libraries and also called pre-defined functions. example - scanf(), printf().

2)            User defined Functions - In the larger program, it becomes extremely important to break program into more manageable pieces called functions. These functions are declared and defined by the programmer in the program.

How to defined a function?

A Function can be defined by two different styles-

1)            Function Declaration- Declaring a function means to write the function prototype above main() or inside the main but before using it. And defining its body just after the curly braces of main().

Syntax

#include<stdio.h>

int sum(int,int);

void main()

{

int x=5, y=6;

sum(x,y);

 

}

int sum(int a, int b)

{

int total;

total=a+b;

printf("The Sum of %d and %d = %d",a,b,total);

return 0;

}

2) Define - A function can be defined without declaring it before main (). These functions must be defined in such a order that

·         The function, that does not call any other function, must be defined first.

·         The function, which may call the first function but not other functions, must be defined second.

·         The function , which may call the first or second functions (or both), must be defined third.

·         That is , the functions are defined in the order that ensures that they are not used in any calling environment before being known  to the compiler.

More info on Functions is available on the Link

You may also like this:

Comments

Most Popular Posts

HTML CSS mcq Question Bank

GST Registration Online

Functions in Excel