Functions

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().

More detailed information is available here

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sanjiv Kumar

Sanjiv Kumar

Blogger, Digital Marketer, Computer Trainer, Affiliate Marketer. Microsoft Certified System Engineer, HP Star Certified Professional for Netservers. BCA, MSCIT,