Module-3
Functions-Designing Structured Programs, user defined function- function definition, function prototype,
function call, Types of functions. Parameter Passing by value, parameter passing by address, Recursive
functions. Dynamic Memory allocation Functions, pointers to functions.
Storage classes-auto, register, static, extern.
Functions
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
A function can also be referred as a method or a sub-routine or a procedure, etc.
Defining a Function
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 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.
Example
Given below is the source code for a function called max(). This function takes two parameters
num1 and num2 and returns the maximum value between the two −
/* 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;
}
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.
A function declaration has the following parts −
return_type function_name( parameter list );
For the above defined function max(), the function declaration is as follows −
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required, so the
following is also a valid declaration −
int max(int, int);
Function declaration is required when you define a function in one source file and you call that
function in another file. In such case, you should declare the function at the top of the file calling
the function.
Calling a Function
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.
When a program calls a function, the program control is transferred to the called function. A
called function performs a defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program control back to the main
program.
To call a function, you simply need to pass the required parameters along with the function
name, and if the function returns a value, then you can store the returned value. For example −
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main () {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* 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;
}
While running the final executable, it would produce the following result −
Max value is : 200
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can
be passed to a function −
Sr.No. Call Type & Description
Call by value
This method copies the actual value of an argument into the formal parameter of the
1
function. In this case, changes made to the parameter inside the function have no
effect on the argument.
Call by reference
This method copies the address of an argument into the formal parameter. Inside
2
the function, the address is used to access the actual argument used in the call. This
means that changes made to the parameter affect the argument.
By default, C uses call by value to pass arguments. In general, it means the code within a
Question on Functions
1.What will be the output of following?
#include <stdio.h>
int main()
{
printf("%d",fun());//calling function
return 0;
}
fun()//called function
{
printf("hello");
}
Output:hello 0
2.Identify valid c statement?
i.int add(int a,b)
ii. add(a,b)=15
iii.return;
Iv. x=add(a+b,a-b);
Ans:iii and iv are valid c statement.
I is not valid because every argument must declared with data type in function decleration
II is not valid because constant value cannot be asigned to functions.
3.what is return value if no type mentioned
int main()
{
printf("%d",fun());
return 0;
}
fun()
{
printf("");
}
Ans:returns 0; because default rfuction eturn type is int
4.what does it mean the following Prototype?
1.int mul(m+5,b);
2.float mul(a,mul(m,n));
3.double mul(a+b,b+c);
4. Void display(void).
Ans;different form of function decleration
5.true/false?
1.if actual parameter are more than formal parameters, extra actual argument discarded.
2.if the actual parameters less than formals then unmatched formal parameter initialized
to garbage value
3.mismatch in data type(actual and formal parameter) result in garbage value.
Ans:all true
Passing value to function
4.progamme to add two number by pass by value method
#include <stdio.h>
int add(int,int);
int main()
{
int a=10,b=20;
printf("%d",add(a,b));
}
int add(int m,int n)
{
return(m+n);
}
5.Add two number pass by reference method
#include <stdio.h>
int add(int*,int*);
int main()
{
int a=10,b=20;
printf("%d",add(&a,&b));
}
int add(int *m,int *n)
{
return(*m+*n);
}
Differnt types of function
1Function with no argument and no return value
example:
fun1()
main()
{
fun();
}
fun()
{
printf("hello");
}
2.Function with argument and no return value
example:
main()
{
fun();
}
fun()
{
printf("%d ",call(5));
}
call(int n)
{
printf("%d ",n);
}
3.Function with no argument but return value
example:
void main()
{
printf("%d ",call());
}
call()
{
return(10*10);
}
4.Function with argument and a return value
example
void main()
{
printf("%d ",call(5));
}
call(int n)
{
return(n+n);
}
5.Function that return multiple value
To send more than one information from called programme to calling programme by using
operator & and *
#include <stdio.h>
int main()
{
int a=30,b=10;
int sum,diff;
find_sumdiff(a,b,&sum,&diff);
printf("%d %d",sum,diff);
return 0;
}
int find_sumdiff(int x,int y,int *m,int *n)
{
*m=x+y;
*n=x-y;
}
Nested functions: function within function
example shows the working of nested functions
1.What will be the output of following?
#include <stdio.h>
int mul(int a,int b);
int add(int b,int c);
void main()
{
int a=3,b=4,c=5,d;
printf("%d",mul(a,add(b,c)));
}
int mul(int a,int b)
{
return(a*b);
}
int add(int b,int c)
{
return(b+c);
}
Ans:27
2.what will be the output of following?
#include <stdio.h>
int mul(int,int);
int main()
{
int a=5,b=4;
printf("%d", mul(a+b,mul(a,b)));
return 0;
}
int mul(int a,int b)
{
return(a*b);
}
ans:180
3.what will be the output of following?
#include <stdio.h>
int mul(int,int);
int main()
{
int a=5,b=4,k;
k=mul(a+b,a-b);
printf("%d",k);
return 0;
}
int mul(int a,int b)
{
return(a+b);
}
ans:10
Recursion
Recursion is the process of repeating items in a self-similar way. In programming languages, if a
program allows you to call a function inside the same function, then it is called a recursive call of
the function.
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
C programming on recusrsion
1.find the factorial of number using recursion
#include <stdio.h>
int main()
{
printf("%d",fact(5));
return 0;
}
fact(int n)
{
if(n==1||n==0)
return 1;
else
return(n*fact(n-1));
}
2.find the fibonacci series
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
printf("%d ",fib(i));
return 0;
}
fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fib(n-1)+fib(n-2));
}
ans:0 1 1 2 3 5 8 13 21 34
Find the sum of digits using recursion
#include <stdio.h>
int main()
{
printf("%d",sum(12345));
return 0;
}
sum(int num)
{
if (num != 0)
{
return (num % 10 + sum(num / 10));
}
else
{
return 0;
}
}
Ans:15
Find hcf using recursion
#include <stdio.h>
int main()
{
printf("%d",hcf(12,24));
return 0;
}
hcf(int n1,int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2);
else
return n1;
}
Ans:12
Find sum of n number
******************************************************************************
*/
#include <stdio.h>
int addNumbers(int n);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d",addNumbers(num));
return 0;
}
int addNumbers(int n)
{
if(n != 0)
return n + addNumbers(n-1);
else
return n;
}
ANS:n=12
sum=72
// A recursive C program to print all numbers from 1 to N without semicoolon
#include<stdio.h>
#define N 10
int main(int num)
{
if (num <= N && printf("%d ", num) && main(num + 1))
{
}
}
Storage class:
A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C Program. They precede the type that they modify. We have four different
storage classes in a C program −
auto
register
static
extern
The auto Storage Class
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with in the same storage class. 'auto' can only be used
within functions, i.e., local variables.
The register Storage Class
The register storage class is used to define local variables that should be stored in a register
instead of RAM. This means that the variable has a maximum size equal to the register size
(usually one word) and can't have the unary '&' operator applied to it (as it does not have a
memory location).
{
register int miles;
}
The register should only be used for variables that require quick access such as counters. It
should also be noted that defining 'register' does not mean that the variable will be stored in a
register. It means that it MIGHT be stored in a register depending on hardware and
implementation restrictions.
The static Storage Class
The static storage class instructs the compiler to keep a local variable in existence during the
life-time of the program instead of creating and destroying it each time it comes into and goes
out of scope. Therefore, making local variables static allows them to maintain their values
between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that
variable's scope to be restricted to the file in which it is declared.
In C programming, when static is used on a global variable, it causes only one copy of that
member to be shared by all the objects of its class.
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main() {
while(count--) {
func();
}
return 0;
}
/* function definition */
void func( void ) {
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
When the above code is compiled and executed, it produces the following result −
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
The extern Storage Class
The extern storage class is used to give a reference of a global variable that is visible to ALL the
program files. When you use 'extern', the variable cannot be initialized however, it points the
variable name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function, which will also be
used in other files, then extern will be used in another file to provide the reference of defined
variable or function. Just for understanding, extern is used to declare a global variable or
function in another file.
The extern modifier is most commonly used when there are two or more files sharing the same
global variables or functions as explained below.
First File: main.c
#include <stdio.h>
int count ;
extern void write_extern();
main() {
count = 5;
write_extern();
}
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void) {
printf("count is %d\n", count);
}
Here, extern is being used to declare count in the second file, where as it has its definition in the
first file, main.c. Now, compile these two files as follows −
$gcc main.c support.c
It will produce the executable program a.out. When this program is executed, it produces the
following result −
count is 5
1.What will be the output of following?
#include<stdio.h>
extern int x=5;
int y=7;
int main()
{
extern int y;
printf("%d %d ", x,y);
return 0;
}
Ans:5 7
2.What will be the output of following?
#include<stdio.h>
extern int x=5;
int y;
int main()
{
extern int y=5;
printf("%d %d ", x,y);
return 0;
}
Error because you cannot initialize global variable within local scope
3.What will be the output of following?
#include<stdio.h>
extern int x=5;
int y;
int main()
{
extern int y;
printf("%d %d ", x,y);
return 0;
}
Ans:5 0 because default value of global variable is 0.
1.What will be the output of following?
#include <stdio.h>
int main()
{
int x=5;
while (x > 0)
{
static int y = 5;
++y;
printf("The value of y is %d\n",y);
x--;
}
}
Ans:
The value of y is 6
The value of y is 7
The value of y is 8
The value of y is 9
The value of y is 10
3.#include<stdio.h>
int recursive(int i)
{
static int count = 0;
count = count + i;
return count;
}
int main()
{
int i, j;
for (i = 0; i <= 5; i++)
j = recursive(i);
printf("%d\n", j);
return 0;}
Ans:15
4.What will be the output of following?
#include<stdio.h>
int num = 5;
int main()
{
fun();
fun();
return 0;
}
int fun()
{
static int num = 2;
printf("%d ",num);
num++;
return 0;
}
Ans:2 3
int num = 2 is static and it executes only for the first time of function execution. Thus outputted 2
5.what will be the output of following?
#include<stdio.h>
extern int x=5;
static int y;
int main()
{
int x;
auto int z;
register int w;
printf("%d %d %d %d ", x,y,z,w);
return 0;
}
Ans:0 0 0 0
6.what will be the output of following?
#include<stdio.h>
int main()
{
static int i;
for(i++;++i;i++)
{
printf("%d ", i);
if(i == 6)
break;
}
return 0;
}
ans:3
7.what will be the output of following?
#include<stdio.h>
int fun();
int main(){
for(fun();fun();fun())
{
printf("%d ", fun()); }
return 0;
}
int fun()
{
int static num = 10;
return num--;
}
8.what will be the output of following?
#include<stdio.h>
int function();
main()
{
int i;
i = function();
printf("%d", i);
return 0;
}
function()
{
int a;
a = 250;
return 0;
}
Output:0
9..what will be the output of following?
#include<stdio.h>
int function();
main()
{
int i;
i = function();
printf("%d", i);
return 0;
}
function()
{
int a;
a = 250;
}
Ans:1
10.what will be the output of following?
#include<stdio.h>
int function(int, int);
int main()
{
int a = 25, b = 24 + 1, c;
printf("%d", function(a, b));
return 0;
}
int function(int x, int y)
{
return (x - (x == y)); }
Ans:24
11..what will be the output of following?
#include<stdio.h>
void fun(int);
int main()
{
int a = 3;
fun(a);
return 0;
}
void fun(int n)
{ if (n > 0)
{
fun(--n);
printf("%d ", n);
}
}
Ans:0 1 2
13.What will be the output of following?
main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Answer:5 4 3 2 1