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

Unit 5

This document provides an overview of functions in C programming, detailing their syntax, types, and advantages. It explains function declarations, definitions, and calls, as well as the distinction between library functions and user-defined functions. Additionally, it covers parameter passing methods, including pass by value and pass by reference, and discusses how to pass arrays to functions.

Uploaded by

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

Unit 5

This document provides an overview of functions in C programming, detailing their syntax, types, and advantages. It explains function declarations, definitions, and calls, as well as the distinction between library functions and user-defined functions. Additionally, it covers parameter passing methods, including pass by value and pass by reference, and discusses how to pass arrays to functions.

Uploaded by

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

Unit-5

A function in C is a set of statements that when called perform some specific task. It is the
basic building block of a C program that provides modularity and code reusability. The
programming statements of a function are enclosed within { } braces, having certain meanings
and performing certain operations. They are also called subroutines or procedures in other
languages.

Syntax of Functions in C
The syntax of function can be divided into 3 aspects:

1. Function Declaration
2. Function Definition
3. Function Calls

Function Declarations
In a function declaration, we must provide the function name, its return type, and the number
and type of its parameters. A function declaration tells the compiler that there is a function
with the given name defined somewhere else in the program.

Syntax

return_type name_of_the_function (parameter_1, parameter_2);


The parameter name is not mandatory while declaring functions. We can also declare the
function without using the name of the data variables.

Example

int sum(int a, int b);


int sum(int , int);
Function Definition
The function definition consists of actual statements which are executed when the function is
called (i.e. when the program control comes to the function).

A C function is generally defined and declared in a single step because the function definition
always starts with the function declaration so we do not need to declare it explicitly. The below
example serves as both a function definition and a declaration.

return_type function_name (para1_type para1_name, para2_type para2_name)


{
// body of the function
}

Function Call
A function call is a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.

In the below example, the first sum function is called and 10,30 are passed to the sum function.
After the function call sum of a and b is returned and control is also returned back to the main
function of the program.
Types of Functions
There are two types of functions in C:

1. Library Functions
2. User Defined Functions

1. Library Function

A library function is also referred to as a “built-in function”. A compiler package already


exists that contains these functions, each of which has a specific meaning and is included in the
package. Built-in functions have the advantage of being directly usable without being defined,
whereas user-defined functions must be declared and defined before being used.
For Example:
pow(), sqrt(), strcmp(), strcpy() etc.

Advantages of C library functions


 C Library functions are easy to use and optimized for better performance.
 C library functions save a lot of time i.e, function development time.
 C library functions are convenient as they always work.

2. User Defined Function

Functions that the programmer creates are known as User-Defined functions or “tailor-made
functions”. User-defined functions can be improved and modified according to the need of the
programmer. Whenever we write a function that is case-specific and is not defined in any
header file, we need to declare and define our own functions according to the syntax.
Advantages of User-Defined Functions
 Changeable functions can be modified as per need.
 The Code of these functions is reusable in other programs.
 These functions are easy to understand, debug and maintain.
Passing Parameters to Functions
The data passed when the function is being invoked is known as the Actual parameters. In the
below program, 10 and 30 are known as actual parameters. Formal Parameters are the variable
and the data type as mentioned in the function declaration. In the below program, a and b are
known as formal parameters.

We can pass arguments to the C function in two ways:

1. Pass by Value
2. Pass by Reference
1. Pass by Value
Parameter passing in this method copies values from actual parameters into formal function
parameters. As a result, any changes made inside the functions do not reflect in the caller’s
parameters.
Example:
C

// C program to show use

// of call by value

#include <stdio.h>

void swap(int var1, int var2)

int temp = var1;

var1 = var2;

var2 = temp;

// Driver code

int main()

{
int var1 = 3, var2 = 2;

printf("Before swap Value of var1 and var2 is: %d, %d\n",

var1, var2);

swap(var1, var2);

printf("After swap Value of var1 and var2 is: %d, %d",

var1, var2);

return 0;

Output

Before swap Value of var1 and var2 is: 3, 2

After swap Value of var1 and var2 is: 3, 2

2. Pass by Reference
The caller’s actual parameters and the function’s actual parameters refer to the same locations,
so any changes made inside the function are reflected in the caller’s actual parameters.

Example:
C

// C program to show use of


// call by Reference

#include <stdio.h>

void swap(int *var1, int *var2)

int temp = *var1;

*var1 = *var2;

*var2 = temp;

// Driver code

int main()

int var1 = 3, var2 = 2;

printf("Before swap Value of var1 and var2 is: %d, %d\n",


var1, var2);

swap(&var1, &var2);

printf("After swap Value of var1 and var2 is: %d, %d",

var1, var2);

return 0;

Output

Before swap Value of var1 and var2 is: 3, 2

After swap Value of var1 and var2 is: 2, 3

Advantages of Functions in C
Functions in C is a highly useful feature of C with many advantages as mentioned below:

1. The function can reduce the repetition of the same statements in the program.
2. The function makes code readable by providing modularity to our program.
3. There is no fixed number of calling functions it can be called as many times as you want.
4. The function reduces the size of the program.
5. Once the function is declared you can just use it without thinking about the internal
working of the function.
Disadvantages of Functions in C
The following are the major disadvantages of functions in C:

1. Cannot return multiple values.


2. Memory and time overhead due to stack frame allocation and transfer of program control.
Passing arrays to functions

In C, there are various general problems which requires passing more than one variable of the
same type to a function. For example, consider a function which sorts the 10 elements in
ascending order. Such a function requires 10 numbers to be passed as the actual parameters from
the main function. Here, instead of declaring 10 different numbers and then passing into the
function, we can declare and initialize an array and pass that into the function. This will resolve
all the complexity since the function will now work for any number of values.

Consider the following syntax to pass an array to the function.

functionname(arrayname);//passing array

Methods to declare a function that receives an array as an argument

There are 3 ways to declare the function which is intended to receive an array as an argument.

First way:

return_type function(type arrayname[])

Declaring blank subscript notation [] is the widely used technique.

Second way:

return_type function(type arrayname[SIZE])

Optionally, we can define size in subscript notation [].

Third way:

return_type function(type *arrayname)

C language passing an array to function example


#include<stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function

int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array

min=minarray(numbers,6);//passing array with size


printf("minimum number is %d \n",min);
return 0;
}

Output

minimum number is 3

You might also like