Functions Functions in C
Functions Functions in C
Functions Functions in C
Functions in C:
In C, a program is made up of one or more functions, one and only one of which must be called
main. The execution of the program always starts with main, but it can call other functions to do some part of the job.
Introduction:
A function is a group of statements that together perform a task. Every C program has at least one function, which
is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is up
to you, but logically the division usually is so each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition
provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. For example, function
strcat() to concatenate two strings .
A function is known with various names like a method or a sub-routine or a procedure, etc.
Several advantages are associated with the use of functions in C or any other language. the major advantages are:
1. Problems can be factored into understandable and manageable steps.
2. Functions provide a way to reuse code that is required in more than one place in a program.
3. Like many languages, C comes with a rich and valuable library.
Ex: math.h
4. We use functions to protect data. Local data consist of data described in a function. These
data are available only to the function and only while the function is executing. When the
function is not running, the data are not accessible. Data in one function, then, cannot be
seen or changed by a function outside of its scope.
Definition of functions:
The general form of a function definition in C programming language is as follows:
return_type function_name( parameter list )
{
body of the function
}
A function definition in C programming language consists of a function header and a function body. Here are all the
parts of a function:
Return Type: A function may return a value. The return_type is the data type of the value the function
returns. Some functions perform the desired operations without returning a value. In this case, the
return_type is the keyword void.
Function Name: This is the actual name of the function. The function name and the parameter list together
constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type,
order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no
parameters.
Function Body: The function body contains a collection of statements that define what the function does.
1
Ex: /* function returning the max between two numbers */
int max(int num1,int num2)
{
/* local variable declaration */
int result;
if(num1 > num2)
result= num1;
else
result= num2;
return result;
}
User defined functions:
In C, functions must be both declared and defined. The function declaration, which needs to be done before
the function call, gives the whole picture of the function that needs to be defined later. The declaration mentions the
name of the function, the return type and the type and order of the formal parameters. In other words, the declaration
uses only the header of the function definition ended in a semicolon. The function definition, which is traditionally
coded after the function that makes the call, contains the code needed to complete the task.
Function Declarations:
A function declaration tells the compiler about a function name and how to call the function. The actual body of the
function can be defined separately.
Function calls:
While creating a C function, you give a definition of what the function has to do. To use a function, you will
have to call that function to perform the defined task.
2
When a program calls a function, program control is transferred to the called function. A called function
performs defined task and when its return statement is executed or when its function-ending closing brace is reached,
it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if function
returns a value, then you can store returned value.
In this category, the function has no arguments. It does not receive any data from the calling function.
Similarly, it doesn’t return any value. The calling function doesn’t receive any data from the called function.
So, there is no communication between calling and called functions.
5
Memory Map
Selection of address for particular variable is compiler dependent and is always whole number. We can access value 10
by using variable name x in this case and by address 65530 also.
‘&’ is a special operator in c used as ‘address of’.
Eg : The expression &x in above case return address of variable x which happens to be 65530.
As address is always positive, we use operator %u for unsigned integer.
‘*’ is special operator in c used as ‘value at address’ also called indirection operator.
Eg : *(&x) which means value at address(&x) i.e. value at address (65530) which happens to be value of variable
x=30.
Ex: #include<stdio.h>
Void main( )
{
int x = 10;
printf ( "\nAddress of x = %u", &x ) ;
printf ( "\nValue of x = %d", x ) ;
printf ( "\nValue of x = %d", *( &x ) ) ;
}
Void exchange(a,b);
void main( )
{
int a = 10, b = 20 ;
printf ( "\na = %d b = %d", a, b ) ;
exchange( &a, &b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
6
Output : a = 10 b = 20
a = 20 b = 10
which means x is declared as pointer variables, i.e. variable is capable of holding address which is always positive
whole number. int * x does not mean that x is going to hold integer value rather it simply means it is going to hold
address (memory location ) of integer type of variable. We are passing address of variables a, b to function
exchange() so while receiving we are using two pointer to integer.
Generally we use call by value mechanism if we don’t need the changes made in calling function to reflect in called
function. But if we required the changes to reflect in called function we use call by reference mechanism.
Also if we want to return more than one value to called function from calling function we go for call by reference value
which is not possible ordinarily.
Recursion :
A function that calls itself is known as recursive function and this technique is known as recursion in C
programming.
The C programming language supports recursion, i.e., a function to call itself. But while using recursion,
programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop.
Recursive function is very useful to solve many mathematical problems like to calculate factorial of a number,
generating Fibonacci series, etc.
7
return n+sum(n-1); /*self call to function sum() */
}
Example: Generating Fibonacci Series
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 0 ; c < n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}