Chapter 4
Input
Function
Output
Function reduces the redundancy of code.It breaks down a large program into small modules to improve readability and portability as well as to make the program easy to debug.
Functions can be categorized into two types:
1. Pre-Defined function
2. User Defined function
Pre-Defined vs User Defined
Pre-Defined Function
2,3
add
3,4
add
4,5
add
5,6
add
11
6,7
add
13
In the above illustration only the input and output arechanging, but the function add remains constant.
So we need not write add the function again and again, instead we can write the add function only once and
call it again with different sets of inputs.
2,3
add
3,4
add
4,5
add
5,6
add
11
6,7
add
13
5.1.4.2. Arguments/Parameter
Arguments are nothing but the input that has been given to the function.The parameters can be passed to a
function by two different methods.
a) Pass by value: In this approach only the copy of the actual variables is passed as aninput to the function.
Hence any modification done inside the function will not be reflected in the actual variable.
b) Pass by reference: In this approach the memory address of the actual variables is passed as a parameter to the function. Hence any modification done inside the function will be reflected in the actual
variable.
Note: Argument should have a valid data type.
Parameter: The names given in the function definition are called parameters.
Argument: The values supplied in the function call are called arguments.
5.1.4.3. Return Type
The return type is nothing but the output or result obtainedafter the execution of the functions.
Using return keyword output can be passed from the function.
When the program control reaches the return keyword it immediately terminates the execution of that
function and transfers the control to the calling function.
Note: A function can return only one value. Return type should be a valid data type.
Function declaration is similar to a variable declaration. It provides the following information to the compiler.
It should contain three parts.
1. The return type(the data type of the result/output)
2. The function name
3. The parameter list(the data type of the inputs)
Some important points in function declaration:
Prototype declaration should always end with a semicolon.
Parameter list is optional.
Default return type is integer.
Note: if there are no parameters or return type in a function, you can specify as void.
Syntax
Return Type Function Name(Argument Type1, Argument Type2, ...., Argument TypeN);
Example
If you are going to create a function name add, that accepts two integer values as input, adds the two
values and gives the result as integer value, then the function declaration would be as follows:
int add(int,int);
Few more examples of function declaration are given below:
Function with no argument and integer as return type
int getValue(void);
Function with integer argument and integer as return type
int square(int);
Function with no argument and no return type
void display(void);
Note:
If function definition is written after the main function, then we need only Prototype Declaration in Global
Declaration Section.
If function definition is written before the main function, then we dont need write Prototype Declaration.
Functions declaration doesnt reserve any memory space.
Example Program
Function Definition Written After Main
#include<stdio.h>
void show();// Function Declaration
void main()
{
displayMessage();
}
void show()
{
printf(After Main);
}
Return Type
Function Name
Local Declaration
Parameter List
(Formal Parameters)
c = a + b;
return (c);
}
Statements
Return Statement
Syntax
ReturnType FunctionName(ArgumenTtype with VariableName)
{
Block of statements;
}
Block of statements may include variable declarations, statements and return value.
Note: The return statement forces the function to return immediately eventhough if function is not yet completed.
Example of function definition
int add(int a,int b)
{
int c;
c = a+b;
return c;
}
Note: In the argument list of function definition, you specify the variable name also.A semicolon should not be placed at
the end of the first line of the function definition.
Another Example
int add(int a,int b)
{
int c;
return c;
c=a+b;
}
Here the function terminates after reaching the return statement without executing the statement return c.
Example Program
#include<stdio.h>
int add(int a,int b) //Function Definition
{
return a+b;
}
void main()
{
int x=10,y=20;
int sum;
sum = add(x,y);// Function Calling
printf(\nSum =%d,sum);
}
Output
30
Example 1
Consider and array:
int arr[4];
arr
arr[0] arr[1] arr[2] arr[3]
In arrays of C programming, name of the array always points to the first element of an array. Here, address of
first element of an array is &arr[0].arr represents the address of the pointer where it is pointing. Hence, &arr[0]
is equivalent to arr.
The statement
int arr[1]={10};
is
equivalent to
int *arr;
arr[0]=10;
arr = &a[0];
Similarly,
&a[1] is equivalent to (a+1)AND, a[1] is equivalent to *(a+1).
In C, you can declare an array and can use pointer to access the elements of an array
Program
//Program to find the sum of six numbers with arrays and pointers.
#include<stdio.h>
void main()
{
int i,class[6],sum=0;
printf("Enter 6 numbers:\n");
for(i=0;i<6;++i)
{
scanf("%d",(class+i));// (class+i) is equivalent to &class[i]
sum +=*(class+i);// *(class+i) is equivalent to class[i]
}
printf("Sum=%d",sum);
}
Output
Enter 6 numbers:
2
3
4
5
3
4
Sum = 21
Example 2
int a[5]={10,20,30,40,50};
Variable
a[0]
a[1]
a[2]
a[3]
a[4]
Value
10
20
30
40
50
1010
1012
1014
1016
1018
Address
Here a[0] is the starting element and the address of a[0] is called the base address
Value of a[0] = 10.
Value of a[1] = 20.
Value of a[2] = 30.
Value of a[3] = 40.
Value of a[4] = 50.
Then Value of a =?
Now what will the variable a hold and what will happen when the value of a is printed?
Consider the statement below:
printf(%d,a);
The output of the statement would be 1010 since an array variable will hold the base address;
Now when we declare a variable as
int a[5];
something equivalent to the following will happen within the compiler implicitly(i.e. the statements will not be
visible but will happen within the compiler )
int *a;// an array variable will be considered as pointer
a = &a[0];// the base address of an array will be stored in the variable a.
Then value of a =&a[0](address of first array variable).
Let us see how the elements of the array are accessed using pointer. As we know the array variable a is also
considered as pointer and it will hold the variable a[0].
If we increment the array variable a, then it will move to the next element.
a=>contains the address of the first element.
*a=>contains the value of the first element.
It means a[0] = 10.
a+1=> contains the address of the second element
*(a+1) =>contains the value of the second element.
It means a[0]+1 = a[1] = 20.
a+2=> contains the address of the third element
*(a+2) =>contains the value of the third element
It means a[0]+2 = a[2] = 30.
a+3=> contains the address of the fourth element
*(a+3) =>contains the value of the fourth element
It means a[0]+3 = a[3] = 40.
a+4=> contains the address of the fifth element.
*(a+4) =>contains the value of the fifth element
It means a[0]+4 = a[4] = 50.
Using dereferencing pointer, we can access the value of elements as follows.
*a or a[0]
10
*(a+1) or a[1]
20
*(a+2) or a[2]
30
*(a+3) or a[3]
40
*(a+4) or a[4]
50
(Actual Parameter)
(Formal Parameter)
10
100
25
1010
1012
1014
1016
The above example illustrates that it is not possible to return two values and without returning the values, the
updated values will not be reflected in the main function.
So using pointer, we pass the address of the value and in the called function when the value is updated, it will
be directly updating in the memory location and so it will be reflected in the main function.
Example 1
#include <stdio.h>
void main()
{
int a=10,b=5;
square(&a,&b);//the address is passed
printf(%d %d,a,b);
}
void square(int *j,int *k)
{
*j=(*j)*(*j); // updation is done directly in the memory location of variablea.
*b=(*b)*(*b); // updation is done directly in the memory location of variableb.
}
Variable
Value
Address
(actual parameter)
(ptr variable)
*j
*k
10
100
25
1010
1012
1010
1012
Explanation:
&a and j refer to same memory location and hence any updates done using the pointer variable j will affect the
value of a.
&b and k refer to same memory location and hence any updates done using the pointer variable k will affect
the value of b.
Output
100 25
Example 1
Using dot(.) operator:
#include <stdio.h>
struct book
{
int pages;
float price;
};
void main()
{
struct book *ptr;
struct book p;
ptr=&p;/* Referencing pointer to memory address of p */
printf(Enter no of pages: );
scanf(%d,&(*ptr).pages); // (*ptr).pages is equivalent to p.pages
printf(Enter price: );
scanf(%f,&(*ptr).price); // (*ptr).price is equivalent to p.price
printf(/nDisplaying:/n );
printf(%d%f,(*ptr).pages,(*ptr).price);
}
Output:
Enter no of pages:450
Enter price: 220.75
Displaying:
450 220.75
In this example, the pointer variable of type struct is referenced to the address of p. Then the structure member
is accessed through pointer using dot operator.
Structure pointer member can also be accessed using -> operator.
(*ptr).a is same as ptr->a
(*ptr).b is same as ptr->b
-> and(*).Bothrepresent the same but operator will not have * in front.
These operators are also used to access data member of structure by using structures pointer.
Example 2
#include <stdio.h>
struct book
{
int pages;
float price;
};
void main()
{
struct book *ptr;
struct book p;
ptr=&p;/* Referencing pointer to memory address of p */
printf(Enter no of pages: );
scanf(%d,&ptr->pages); // (*ptr).pages is equivalent to p.pages
printf(Enter price: );
scanf(%f,&ptr->price); // (*ptr).price is equivalent to p.price
printf(/nDisplaying:/n );
printf(%d %f,ptr->pages,*ptr->price);
}
Output:
Enter no of pages:450
Enter price: 220.75
Displaying:
450 220.75
Example
void *ptr;
Consider the below example to clearly understand the void pointer:
void *ptr;// ptr is declared as Void pointer
char cnum;
int inum;
float fnum;
ptr = &cnum;// ptr has address of character data
ptr = &inum; // ptr has address of integer data
ptr = &fnum;// ptr has address of float data
Explanation
We have declared three variables of integer,character and float type.
When we assign address of integer to the void pointer, pointer will become Integer Pointer.
When we assign address of Character Data type to void pointer it will become Character Pointer.
When we assign address of floating point Data type to void pointer it will become floating point Pointer
It is capable of storing address of any data type
(a == NULL )
(b == NULL )
ALWAYS true
Example program:
Void main()
{
int *a;
int *b =NULL;
printf(value of a =%d \t value of b =%d,a,b);
}
Output:
Value of a = 28620 value of b = 0
NULL is a reserved word in most high-level languages. It is an EXPLICIT value, and not anundefinedvalue.
5000
25
8000
5000
ptr
5000
25
8000
5000
Later:
void pointer
NULL pointer
wild pointer
dangling pointer
It points to a memory
address of a variable,
which has been deleted