0% found this document useful (0 votes)
25 views

Function in C

Uploaded by

smritibhardwaz4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Function in C

Uploaded by

smritibhardwaz4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT

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

Defn: A function is a block of statements that performs a specific task by


taking data as argument, perform computation on it and return the result back
to the function call.

Why we need functions in C


Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any program
rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to
be traced.
d) Reduces the size of the code.

Types of functions

1) Predefined standard library functions: – These are the functions which


already have a definition in header files (.h files like stdio.h), so we just call
them whenever there is a need to use them.

.For Example: puts(), gets(), printf(), scanf() etc.

2) User Defined functions – The functions that we create in a program


are known as user defined functions.
Parts of a function:

1. Function declaration/Prototype.

2. Function Calling.

3. Function Definition.

1.)Function Declaration:

Syntax:

<return type > <function name>([<type of argument>])

The declaration of function name, its argument and return type is called function
declaration.

2.) Function Calling:

The process of calling a function for processing is called function calling.

(i): without returning value

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:

<return type> <function name>([<type of arguments> or <argument list>])

<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>

int cube(int n);

void main()

int c, n;

printf("Enter a no.");

scanf("%d",&n);

c=cube(n);

printf("cube of a no. is=%d",c);

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.

a.) Actual 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

b.) Formal Parameters.


These are the parameters which are used in function defination for
processing.
Example: int fact(int x) //here x is the formal parameter
{
…….
}
Example: int sum(int x, int y) //here x and y are the formal parameters
{
…….
}

Passing parameters to a function:

Methods of parameters passing:


1.) Call by value/Pass by value
2.) Call by reference. /Pass by reference
1) Call by value.

 This is the default method of passing parameter in c.


 In this method of parameter passing, duplicate values or copy of parameters
are passed from calling program to function defination.
 In this case the values of actual parameters are copied to the formal
parameters.
 Thus, Any change made in function would not be reflected back to the calling
program.

Example: WAP to demonstrate the swapping of two numbers by using function


call by value method.

Program based upon call by value:

# 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;
}

2.) Call by reference:

 In this method of parameter passing, original values of variables are passed


from calling program to function.
 The addresses of actual parameters are passed to the formal parameter.
 Thus, any change made in the function definition, can be reflected back to the
calling program because all the operations performed on the value stored in
the address of actual parameters.

Example: WAP to demonstrate the swapping of two numbers by using


function call by reference method.

Program based upon call by reference:


# include<stdio.h>
void swap(int *x, int *y);
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);
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;
}
Passing Array to Function:
Passing Single Dimension array to function in C

(i) Passing individual elements of array


(ii) Passing an entire array to a function

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.

Example-1: Passing individual elements of 1D array to function

#include<stdio.h>

void disp(int c1,int c2);

void main()

int arr[]={1,3,5,4,9};

disp(arr[0],arr[4]);

void disp(int c1,int c2)

printf(“%d %d”,c1,c2);

}
Example 2.1: Passing an entire 1D array to function

Q: WAP to find the sum of all elements of an array using function

#include<stdio.h>

int disp(int a[],int x);

void main()

Int s;

int arr[]={1,3,5,4,9};

s=disp(arr,5);

printf(“sum of all elements is %d”,s);

int disp(int a[],int x)

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 disp(int *a, int x);

void main()

int s;

int arr[]={1,3,5,4,9};

s=disp(arr,5);

printf(“sum of all elements is %d”,s);

int disp(int *a,int x)

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 disp( char *x, int y);

void main()

char arr[]={‘a’,’e’,’i’,’o’,’u’};

disp(arr,5);

void disp( char *x, int y)

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.

Passing multi-dimensional array to a function

The simplest approach to pass a two-dimensional array to a function is to specify the


second dimension of the array,
Example-1: Program to find the sum of all elements in 2D array using function.
[if dimension of the array is known at compile time]

#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]);
}

void dblaryfun(int r,int c,int x[])


{
int s=0,i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
s=s+x[i*c+j];
}
}
printf("sum of all elements is %d",s);

You might also like