Functions in C Programming With Examples

What is a Function?

A Function is a block of statements that performs a specific task of some kind. Every C program can be thought of as the Collection of these Functions. The below Example shows how we can write a simple function.

 

Function Example

In the above Example, we have made a function. After creating the Function we can call it from main(). Functions can also call each other. A function can even call itself. The Function which calls itself is known as Recursion.

Conclusions that can be drawn from above Program

  1. A C program is a Collection of one or more Functions.
  2. If a C program contains only one function, it must be main().
  3. If a C program contains more than one function, then one of them is main() because the program execution begins with main().
  4. There can be as many functions in a program as possible.

Why do we need Functions?

  • It divides the program into separate well-defined functions so that each function can be written and tested separately.
  • Understanding, coding and testing becomes very easy as they are separated.
  • Writing functions avoids rewriting the same code over and over.
  • As the functions are divided the workload can also be divided among the programmers.

Function Declaration in C

 

Function Declaration

Basic Syntax:


return_data_type function_name (data_type var1, data_type var2, …..);

Where,

function_name: the name for the function should be valid. It should be a meaningful name that should clarify what all tasks it will perform.

return_data_type: it is used for specifying the data type of the value that is returned to the calling function after the processing.

data_type var1, data_type var2: function arguments and their data types.

Return Data Types

The return value has a type as other values in C. It can be int, float, char or anything else. The type of return value determines the type of your function.

The default return type of function is int or integer.

In the above Program, we have 3 Functions:

  1. multiply(): Its return type is int, therefore it will return an integer type value.
  2. print(): Its return type is void, therefore it will not return anything, it will simply execute the code within.
  3. divide(): Its return type is a float, therefore it will return a decimal type value.

Function Definition in C

Whenever a function is defined, its space is allocated in the memory.

Syntax:


return_data_type function_name (data_type var1, data_type var2, …..);
{
        …...................
        statements;
        …...................
        return (variable);
}

The statements written within the curly braces ({}) are the body of the function which contains the code to be performed.

Calling a Function in C

 

Function Working

Whenever the function is invoked the compiler skips on to the called function for executing its statements. We mean that the control passes to the function The activity of main() is temporarily suspended.

Once the called function is executed the control of the program is passed back to the calling function.

Passing values between functions

 

Passing Values In C

In the above Program, the variable a, b, c are called actual arguments. The variables x, y, z are called as formal arguments.

Call by Value in C

In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.

While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”.

C program to illustrate call by value


#include<stdio.h>
void swapx(int x, int y);
int main()
{
	int a = 100, b = 200;
	// Pass by Values
	swapx(a, b);
	printf("a=%d b=%dn", a, b);
	return 0;
}
void swapx(int x, int y)
{
	int t;
	t = x;
	x = y;
	y = t;
	printf("x=%d y=%dn", x, y);
}

Output:

 

The actual values of a and b remain unchanged even after exchanging the values of x and y.

Call by Reference in C

While calling a function, instead of passing the values of variables, we pass the address of variables (pointers) to the function known as “Call By References.

Both the actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in the actual parameters of the caller.

C program to illustrate Call by Reference


#include<stdio.h>
void swapx(int*, int*);
int main()
{
	int a = 100, b = 200;
	// Pass reference
	swapx(&a, &b);
	printf("a=%d b=%dn", a, b);
	return 0;
}
void swapx(int* x, int* y)
{
	int t;
	t = *x;
	*x = *y;
	*y = t;
	printf("x=%d y=%dn", *x, *y);
}

Output:

 

Call By Refrence

The actual values of a and b get changed after exchanging values of x and y.

What is a Recursive Function?

A function which calls itself is called a Recursive function.

While using the recursive functions, it is important to be careful to define the exit condition from the function or then it may result in an infinite loop.

They are used for calculating factorial of a number, Fibonacci series, etc.


#include<stdio.h>
int fib(int n)
{
if (n <= 1)
	return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}

By admin

Leave a Reply

%d bloggers like this: