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

Functions

The document discusses functions in C programming. It explains what functions are, the need for functions, advantages of using functions, types of functions including library functions and user-defined functions. It also covers function declaration, definition, calling, parameters, return values, and call by value vs call by reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Functions

The document discusses functions in C programming. It explains what functions are, the need for functions, advantages of using functions, types of functions including library functions and user-defined functions. It also covers function declaration, definition, calling, parameters, return values, and call by value vs call by reference.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Functions

Need of a Function
Knowingly or unknowingly we rely on so many persons for so
many things. Man is an intelligent species, but still cannot
perform all of life’s tasks all alone. He has to rely on others.
For Example:
You may call a mechanic to fix up your bike, rely on a store to
supply you groceries every month.
A computer program finds itself in a similar situation. It
cannot handle all the tasks by itself. Instead, it requests other
program like entities—called ‘functions’ in C—to get its tasks
done. So now we will look at a variety of features of these
functions, starting with the simplest one and then working
towards those that demonstrate the power of C functions.
What is a Function?

In C, we can divide a large program into the basic


building blocks known as function. The function
contains the set of programming statements enclosed
by {}. A function can be called multiple times to
provide reusability and modularity to the C program. In
other words, we can say that the collection of functions
creates a program. The function is also known
as procedure or subroutine in other programming
languages.
For Example:
Suppose you have a task that is always performed exactly
in the same way—say a bimonthly servicing of your
motorbike. When you want it to be done, you go to the
service station and say, “It’s time, do it now”. You don’t
need to give instructions, because the mechanic knows his
job. You don’t need to be told when the job is done. You
assume the bike would be serviced in the usual way, the
mechanic does it.
A simple C function also operates in much the same way
as the mechanic. we will be look at two things—a function
that calls or activates the function and the function itself.
Advantages of Functions
 By using functions, we can avoid rewriting same
logic/code again and again in a program.
 We can call C functions any number of times in a

program and from any place in a program.


 We can track a large C program easily when it is

divided into multiple functions.


 Reusability is the main feature of C functions.
Types of Functions

There are two types of functions in C programming:


 Library Functions: are the functions which are

declared in the C header files such as scanf(), printf(),


gets(), puts(), ceil(), floor() etc.
 User-defined functions: are the functions which are

created by the C programmer, so that he/she can use


it many times. It reduces the complexity of a big
program and optimizes the code.
C Library Functions
 Library functions are the inbuilt function in C that are grouped
and placed at a common place called the library. Such functions
are used to perform some specific operations.
 For example, printf is a library function used to print on the
console. The library functions are created by the designers of
compilers.
 All C standard library functions are defined inside the different
header files saved with the extension .h. We need to include these
header files in our program to make use of the library functions
defined in such header files.
 For example, To use the library functions such as printf/scanf we
need to include stdio.h in our program which is a header file that
contains all the library functions regarding standard input/output.
Library Functions Examples:
User Defined Function
C allows you to define functions according to your
need. These functions are known as user-defined
functions.
For example:
Suppose, you need to create a circle and color it
depending upon the radius and color. You can create
two functions to solve this problem:
 createCircle() function
 color() function
Example of simple user defined function:
#include<stdio.h>
void message();// function declaration
int main()
{
printf(“Hello”);
message();
return 0; // function calling
}
void message() // function definition
{
printf(“World”);
}

Output:
Hello World
C User defined Function Aspects

There are 3 aspects in each C function. They are:


 Function declaration
 Function call
 Function definition
Function declaration
A function must be declared globally in a C program to
tell the compiler about the function name, function
parameters, and return type. Function declaration is also
called Function Prototyping.

The syntax of declaring a function is:

return_type function_name(data_type parameter


...);
Void message();
Void message(int a, int b);
Function calling
Function can be called from anywhere in the program. The
parameter list must watch in function calling and function
declaration. We must pass the same number of functions as it
is declared in the function declaration.

The syntax of calling a function is:


function_name(parameter...);
Function definition
It contains the actual statements which are to be executed.
It is the most important aspect to which the control comes
when the function is called. Here, we must notice that only
one value can be returned from the function.

The syntax of defining a function is:

return_type function_name(data_type parameter.


..){
//code to be executed
}
Return Value
A C function may or may not return a value from the function. If you don't have to
return any value from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the
function.
Example without return value:
void hello(){
printf("hello C");
}

If you want to return any value from the function, you need to use any data type
such as int, long, char, etc. The return type depends on the value to be returned
from the function.
Example with return value:
int get(){
return 10;
}
Different ways of function calling

A function may or may not accept any argument.


It may or may not return any value. Based on
these facts, There are four different aspects of
function calls.
 function without arguments and without return

value
 function without arguments and with return value
 function with arguments and without return value
 function with arguments and with return value
Example for Function without argument and
without return value

#include<stdio.h>
void printName(); //function declaration
int main ()
{
printf("Hello ");
printName(); //fun call
return 0;
}
void printName() //fun definition
{
printf(“World");
}

Output:
Hello World
Example for Function without arguments and with return
value
#include<stdio.h>
int sum(); //fun decl
int main()
{
int a,b;
int result;
printf("\nGoing to calculate the sum of two numbers:");
result = sum(); //fun call
printf("%d”,result);
return 0;
}
int sum() //fun defi 11
{
int a, b;
printf("\nEnter two numbers");
scanf("%d %d”,&a,&b); 5 6
return a+b; 5+6=11
}
Output:
Going to calculate the sum of two numbers:
Enter two numbers
45
9
Example for Function with argument and without return value

#include<stdio.h>
void sum(int , int); //fun declare
int main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b); //fun call
return 0;
}
void sum(int a, int b) //fun defin
{
printf("\nThe sum is %d",a+b);
}
Output:

Going to calculate the sum of two numbers:


Enter two numbers
46
The sum is 10
Example for Function with argument and with return value
#include<stdio.h>
int sum(int, int);
int main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
return 0;
}
int sum(int a, int b)
{
return a+b;
}
Output:
Going to calculate the sum of two numbers:
Enter two numbers
48
The sum is: 12
Actual and Formal parameters
Actual Parameters: The values/variables passed while
calling a function are called actual parameters.
Formal Parameters: These are the
variables written/declared in function
definition/prototype, and receive their values when a
call to that function is made.

The value(s) of the actual parameters are copied to


formal parameters when the call to that function is
made.
Int main()
{

Sum(a,b);//actual parameters

}
Int sum(int a,int b) //formal parameters
{
}
Call by value and Call by reference in C

There are two methods to pass the data into the function in C language,
i.e., call by value and call by reference.

Call by value in C
1. In call by value method, the value of the actual parameters is copied
into the formal parameters.
2. In call by value method, we can not modify the value of the actual
parameter by the formal parameter.
3. In call by value, different memory is allocated for actual and formal
parameters since the value of the actual parameter is copied into the
formal parameter.
4. The actual parameter is the argument which is used in the function
call whereas formal parameter is the argument which is used in the
function definition.
Example of call by value in c
#include<stdio.h>
void change(int num);
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x); //passing value in function
printf("After function call x=%d \n", x);
return 0;
}

Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
Call by Value Example: Swapping the values of the two variables

#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); //printing value of a & b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); //Actual parameters a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\
n",a,b); // Formal parameters a = 20, b =10
}
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
Call by reference in C

 In call by reference, the address of the variable is


passed into the function call as the actual parameter.
 The value of the actual parameters can be modified by

changing the formal parameters since the address of the


actual parameters is passed.
 In call by reference, the memory allocation is similar

for both formal parameters and actual parameters. All


the operations in the function are performed on the
value stored at the address of the actual parameters, and
the modified value gets stored at the same address.
Example of call by reference in C
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing a & b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);//Actual
parameters a = 20, b = 10
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
// printf("After swapping values in function a = %d, b = %d\n",*a,*b); //
Formal parameters, a = 20 b = 10
}
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
Difference between call by value and
call by reference
Parameters Call by value Call by reference

Definition While calling a function, when you While calling a function, in


pass values by copying variables, it is programming language instead of
known as "Call By Values." copying the values of variables, the
address of the variables is used it is
known as "Call By References.
Arguments In this method, a copy of the variable In this method, a variable itself is
is passed. passed.
Effect Changes made in a copy of variable Change in the variable also affects the
never modify the value of variable value of the variable outside the
outside the function. function.
Alteration of value Does not allow you to make any Allows you to make changes in the
changes in the actual variables. values of variables by using function
calls.
Passing of variable Values of variables are passed using a Pointer variables are required to store
straightforward method. the address of variables.
Original value not modified. The original value is modified.
Value modification

Memory Location Actual and formal arguments will be Actual and formal arguments will be
created in different memory location created in the same memory location

You might also like