0% found this document useful (0 votes)
8 views5 pages

Functions

C functions are modular blocks of code that perform specific tasks, enhancing code reusability. They consist of declarations, definitions, and calls, and can be categorized into library functions and user-defined functions. Parameters can be passed by value or by reference, each with distinct behaviors regarding memory and variable manipulation.

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Functions

C functions are modular blocks of code that perform specific tasks, enhancing code reusability. They consist of declarations, definitions, and calls, and can be categorized into library functions and user-defined functions. Parameters can be passed by value or by reference, each with distinct behaviors regarding memory and variable manipulation.

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Functions

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.
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 Declaration
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);//function declaration


#include <stdio.h>

int sum(int a, int b) //function definition


{
return a + b;
}
int main()
{
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
Function Definition
The function definition consists of actual statements which are executed when
the function is called. The programming statements of a function are enclosed
within { } braces, having certain meanings and performing certain operations.
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”. 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.
 C library functions are convenient as they always work.
Passing Parameters to Functions
We can pass arguments to the C function in two ways:
1. Pass by Value (Call by value)
2. Pass by Reference (Call by reference)
Difference Between Call by Value and Call by Reference

Functions can be invoked in two ways: Call by Value or Call by Reference.


These two ways are generally differentiated by the type of values passed to
them as parameters.
The parameters passed to the function are called actual parameters whereas
the parameters received by the function are called formal parameters.
Call By Value
In call by value method of parameter passing, the values of actual parameters
are copied to the function’s formal parameters.
 There are two copies of parameters stored in different memory
locations.
 One is the original copy and the other is the function copy.
 Any changes made inside functions are not reflected in the actual
parameters of the caller.
Example:
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
// printing the value of a and b in main.
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b); //The actual parameters do not change by changing the formal
parameters in call by value.
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int a, int b) //formal parameters
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

Pass by Reference(Call by reference)


In call by reference method of parameter passing, the address of the actual
parameters is passed to the function as the formal parameters.
Both the actual and formal parameters refer to the same locations.
 Any changes made inside the function are actually reflected in the
actual parameters of the caller.
Example:
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
// printing the value of a and b in main.
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
// Formal parameters, a = 20, b = 10
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10

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.

You might also like