Function in C
Function in C
Lecture Notes
Topic: Function
Points to be discussed:
1. Function Definition
2. Use of function
3. Types of Function
4. Parts of Function
5. Arguments vs Parameters
6. Passing parameters to a Function
7. Methods of parameters passing
8. Passing Single Dimension array to function in C
9. Passing Multi-Dimension array to Function in C
Types of functions
1. Function declaration/Prototype.
2. Function Calling.
3. Function Definition.
1.)Function Declaration:
Syntax:
The declaration of function name, its argument and return type is called function
declaration.
Syntax: function_name(variable1,variable2,…..)
Example: sum(a,b);
(ii) while returning value
Syntax: variable=function_name(variable1,varible2,…..);
Example: s=sum(a,b);
Here s stores the result returned by the called function.
3.) Function definition:
The process of writing a code for performing any specific task is called function
definition.
Syntax:
<Block of code>
[ return(<vlaue>)]
return_type: Return type can be of any data type such as int, double,
char, void, short etc.
function_name: It can be anything for naming the function based on
identifier naming rule.
argument list: Argument list contains variables names along with their
data types separated by comma, but it is optional.
Block of code: Set of C statements, which will be executed whenever a
call will be made to the function.
return statement: this is optional. It is used to return the result of
function computation to the calling function.
Example: (i) returning value
int sum(int x,int y) //function prototype
{ // function body
int z;
z=x+y;
return(z);
}
Example: (ii) without returning value
void sum(int x,int y) //function prototype
{ // function body
int z;
z=x+y;
printf(“sum is %d”,z);
}
Example: WAP to enter two numbers and find the sum of two numbers
using function.
#include<stdio.h>
int sum(int,int); //function prototype
void main()
{
int a,b,s;
printf(“enter 1st number”);
scanf(“%d”,&a);
printf(“enter 2nd number”);
scanf(“%d”,&b);
s=sum(a,b); //function call
printf(“sum is %d”,s);
}
//function defination
int sum(int x,int y)
{
int z;
z=x+y;
return(z);
}
Q 1: WAP to compute cube of a number using function.
Q 2: WAP to find the factorial of a number using function
Q 3: WAP to find the sum, difference, product of two numbers using
function.
Solution 1.WAP to compute cube of a no. using function.
#include<stdio.h>
void main()
int c, n;
printf("Enter a no.");
scanf("%d",&n);
c=cube(n);
int cube(int n)
int r;
r=n*n*n;
return(r);
Arguments vs Parameters
Arguments:
An argument is a value passed to a function when the function is called. Whenever
any function is called during the execution of the program there are some values
passed with the function. These values are called arguments.
Parameters:
A parameter is a variable used to define a particular value during a function
definition. Whenever we define a function we introduce our compiler with some
variables that are being used in the running of that function. These variables are
termed as Parameters. The parameters and arguments mostly have the same value
but theoretically, are different from each other.
Types of parameter:
There are 2 types of parameters:
a.) Actual Parameters.
b.) Formal Parameters.
These are the parameters which are used in main() function for function
calling. It appears in function call.
Example: f=fact (n); // here n is the actual parameter
Or s=sum(a,b); // here a and b are the actual parameters
# include<stdio.h>
void swap(int,int);
void main()
{
int a,b;
a=10; b=20;
printf("The value of a before swapping=%d",a);
printf("The value of b before swapping=%d",b);
void swap(a,b);
printf("The value of a after swapping=%d",a);
printf("The value of b after swapping=%d",b);
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
Just like variables, array can also be passed to a function as a parameter. Arrays in
C are passed as reference not by value. Which means any changes to array within
the function will also persist outside the function.
#include<stdio.h>
void main()
int arr[]={1,3,5,4,9};
disp(arr[0],arr[4]);
printf(“%d %d”,c1,c2);
}
Example 2.1: Passing an entire 1D array to function
#include<stdio.h>
void main()
Int s;
int arr[]={1,3,5,4,9};
s=disp(arr,5);
int i,z;
for(i=0;i<x;i++)
z=z+a[i];
return(z);
}
Example-2.2: Passing 1D array to function as pointer
#include<stdio.h>
void main()
int s;
int arr[]={1,3,5,4,9};
s=disp(arr,5);
int i,z;
for(i=0;i<x;i++)
z=z+a[i];
return(z);
}
Example 2.3 Passing character array to function
#include<stdio.h>
void main()
char arr[]={‘a’,’e’,’i’,’o’,’u’};
disp(arr,5);
int i;
for(i=0;i<y;i++)
printf(“%c”, x[i]);
Q 6: WAP to count the total number of even number present in an array using function.
Q 7: WAP to create two one dimensional arrays of size 5 and find the sum of two arrays using
function.
Q 8: WAP to print the largest and smallest number present in the array by passing it as parameter
to a function.
Q 9. WAP to create a single dimension array of size 10, store element into it and print all elements
in the array in ascending order by passing it as parameter to function.
#include<stdio.h>
void dblaryfun(int x[2][3]);
void main()
{
int i,j;
int arr[][3]={{1,2,3},{4,5,6}};
dblaryfun(arr);
}
void dblaryfun(int x[2][3])
{
int s=0,i,j;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
s=s+x[i][j];
}
}
printf("sum of all elements is %d",s);
}
Example-2: Program to find the sum of all elements in 2D array using function.
[if dimension of the array is not known at compile time]
#include<stdio.h>
void dblaryfun(int r,int c,int x[][3]);
void main()
{
int i,j;
int arr[][3]={{1,2,3},{4,5,6}};
dblaryfun(2,3,arr);
}
void dblaryfun(int r,int c,int x[][3])
{
int s=0,i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
s=s+x[i][j];
}
}
printf("sum of all elements is %d",s);
}
Example-3:
Example-2: Program to find the sum of all elements in 2D array using function.
[2D arrays can be passed and processed using 1D array]
#include<stdio.h>
void dblaryfun(int r,int c,int x[]);
void main()
{
int i,j;
int arr[][3]={{1,2,3},{4,5,6}};
dblaryfun(2,3,arr[0]);
}