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

C lecture 4.1

Functions in C are groups of statements that perform tasks, with every program requiring at least one function, main(). They improve code readability, reusability, and debugging ease, and can be categorized into predefined standard library functions and user-defined functions. Parameters can be passed to functions in various ways, including by value and by reference, allowing for flexible data manipulation.

Uploaded by

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

C lecture 4.1

Functions in C are groups of statements that perform tasks, with every program requiring at least one function, main(). They improve code readability, reusability, and debugging ease, and can be categorized into predefined standard library functions and user-defined functions. Parameters can be passed to functions in various ways, including by value and by reference, allowing for flexible data manipulation.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions: A function is a group of statements that together perform a task.

Every C program has at least one function, which


is main(), and all the most trivial programs can define additional functions.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides
the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate
two strings, memcpy() to copy one memory location to another location, and many more functions.

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, duplicate set of statements are replaced by function calls.
Types of functions:

1) Predefined standard library functions


Standard library functions are also known as built-in functions. Functions such as puts(), gets(), printf(), scanf() etc are
standard library functions. These functions are already defined in header files (files with .h extensions are called header files
such as stdio.h), so we just call them whenever there is a need to use them.
For example: printf() function is defined in <stdio.h> header file so in order to use the printf() function, we need to include
the <stdio.h> header file in our program using #include <stdio.h>.

2) User Defined functions


The functions that we create in a program are known as user defined functions or in other words you can say that a function
created by user is known as user defined function.
Syntax of a function:
return_type function_name (argument list)
{
Set of statements – Block of code
}

return_type: Return type can be of any data type such as int, double, char, void, short etc.

function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be
easy to understand the purpose of function just by seeing it’s name.

argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the
function. For example – A function which is used to add two integer variables, will be having two integer argument.

Block of code: Set of C statements, which will be executed whenever a call will be made to the function.
Function Declaration: declaration is giving a prototype like simply a name .
return_type function_name();
ex- int sum();
In the declaration we just provide function name,return not write body.

Function Definition: definition is associating the task or the meaning with the prototype. Here we write the body of function
what the work.
ex-
int sum(){
c=a+b;
return c;
}

Function Call:A function call is an expression that passes control and arguments (if any) to a function and has the form:
expression (expression-listopt)
If the function does not return a value, returns void.
Prototype Declaration: A function prototype is simply the declaration of a function that specifies function's name,
parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that
the function may later be used in the program.
The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of
a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks
the function signatures before calling it.
Example:
#include<stdio.h>
void function(int); //prototype
main()
{
function(50);
}
void function(int x)
{
printf("The value of x is: %d", x);
}
Passing Parameters through Functions:
There 4 types to pass parameters through a function. These are:

a) No argument-no return type (no parameter passing , void as data type)


b) Argument but no return type (parameter passing , void as data type)
c) No argument but return type (no parameter passing, a data type must be)
d) Argument and return type (parameter passing, a data type must be)

A function can take parameters which are just values you supply to the function so that the function can do something utilizing
those values. These parameters are just like variables except that the values of these variables are defined when we call the
function and are not assigned values within the function itself.

Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the
function, we supply the values in the same way. Note the terminology used - the names given in the function definition are
called parameters whereas the values you supply in the function call are called arguments.
No argument-no return type :
#include<stdio.h>
void add(void);
int main()
{
add();
return 0;
}
void add(void)
{
float x;
int y;
printf(" enter two number:\n ");
scanf("%f%d",&x,&y);
printf("\n division=%f",x+y);
}
Argument but no return type :
#include<stdio.h>
void mul(float,int);
int main()
{
float x;
int y;
printf("enter two number:\n");
scanf("%f%d",&x,&y);
mul(x,y);
return 0;
}
void mul(float a,int b)
{
printf("\n multiplication is=%f",a*b);
}
No argument but return type :
#include<stdio.h>
float div(void);
int main()
{
float r;
r=div();
printf("\n division=%f",r);
return 0;
}
float div(void)
{
float x;
int y;
printf("\n enter two numbers:");
scanf("%f%d",&x,&y);
return(x/y);
}
Argument and return type :
#include<stdio.h>
float sub(float,int);
int main()
{
float r,x;
int y;
printf("\n enter two numbers:");
scanf("%f%d",&x,&y);
r=sub(x,y);
printf("\n subtraction=%f",r);
return 0;
}
float sub(float a,int b)
{
return(a-b);
}
Function arguments in c programming :

Here, as shown in the figure above arguments value is used to send values to the called program.
Basically, there are two types of arguments:

• Actual arguments: the values that are passed to the called function from the main function are known as Actual
arguments.
• Formal arguments : The variables declared in the function prototype or definition are known as Formal arguments

The actual arguments and formal arguments must match in number, type, and order.

Following are the two ways to pass arguments to the function:


• Pass by value : Pass by value is a method in which a copy of the value of the variables is passed to the function for the
specific operation. In this method, the arguments in the function call are not modified by the change in parameters of the
called function. So the original variables remain unchanged.
• Pass by reference : Pass by reference is a method in which rather than passing direct value the address of the variable is
passed as an argument to the called function.
When we pass arguments by reference, the formal arguments in the called function becomes the assumed name or aliases of
the actual arguments in the calling function. So the function works on the actual data.
Pass by value :
# include <stdio.h>
int add (int a, int b)
{
return( a + b );
}

int main()
{
int x, y, z;
x = 5;
y = 5;
z = add(x,y); // call by value
printf("\n result=%d",z);
return 0;
}
In this program, function add() is called by passing the arguments x and y. The copy of the values of x and y are passed to a and b respectively and then are
used in the function. So by changing the values of a and b, there will be no change in the actual arguments x and y in the function call.
Pass by reference :
#include <stdio.h>
void swap (int *a, int *b) // a and b are reference variables
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 2, y = 4;
printf("before swapping x = %d and y = %d\n", x, y);
swap(&x, &y); // call by reference
printf("after swapping x = %d and y = %d\n", x, y);
return 0;
}
In the above program, the formal arguments a and b becomes the alias of actual arguments x and y when the function was called. So when the
variables a and b are interchanged x and y are also interchanged.

You might also like