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

Functions

The document discusses functions in C programming. It defines what a function is, the advantages of using functions, and different types of functions like predefined and user-defined functions. It also explains function declaration, definition, calling and different ways of passing arguments to functions like call by value and call by address.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Functions

The document discusses functions in C programming. It defines what a function is, the advantages of using functions, and different types of functions like predefined and user-defined functions. It also explains function declaration, definition, calling and different ways of passing arguments to functions like call by value and call by address.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Functions

Harapriya Mohanta
Function:
A function is a group of statements(instructions) that performs a particular and
predefined task.
The idea behind the function is to put the instructions(performs a specific task)
and make a function so that instead of writing the same code again and again,
we can call the function.

Advantages:
▪ Code reusability
▪ Easier to debug
▪ Divide the large program into several sub-programs(using divide-and-conquer
principle) and provide better memory utilization as well.
▪ It reduces the size of the program due to code reusability.

Harapriya Mohanta
Types of Function:

Function

Pre-defined User-defined

scanf() my_fun()
printf() add()
square(), etc.

Harapriya Mohanta
Pre-defined(Library) Function:
These functions are already dfined on the C library and we can simply use
them by including the appropriate header files in our program. Some of
the common used pre-defined functions are scanf(), printf(). gets(), etc.

User-defined functions:
Functions that are defined ny user according to their needs to perform
some specific task are known as User-defined functions.

Harapriya Mohanta
Is the main() function is predefined or use-defined?

The main() function is a user-defined with a predefined format.


As the user(programmer) defines it, it is a user-defined function, but it has a special
status among other user-defined functions.

As the program’s execution starts from the main() function, there is a predefined
standard or format that is present so that the compiler knows about it.

Harapriya Mohanta
How to make our own function?

Three things are essential while defining a function:

1. What inputs(arguments) does the function take?


2. What task the function is performing?
3. What it returns after completing its work?

Whether there is a need to take parameters or to return something, it purely


depends on the scenario for which the function is created.

Harapriya Mohanta
Function Declaration(prototype) & Definition:
Declaration
return_type function_name(arg1, arg2,-----);

int add (int x, int y); //declaration or prototype

Definition

int add (int x, int y)


{
function Definition
//function body

Harapriya Mohanta
How to use function?

There are two ways to use functions.


1. Define first, then use
2. Declare first, then use, then Define

Harapriya Mohanta
1. Define first, then use 2. Declare first, then use, then Define
#include<stdio.h> #include<stdio.h>
void fun() void fun();
{ int main()
printf(“i am in fun() function”); {
} ------
int main() fun();
{ ------
------ }
fun(); void fun()
------ {
} printf(“i am in fun() function”);
}

Harapriya Mohanta
What is the purpose of function declaration?

What will happen if i remove the declaration?

Harapriya Mohanta
Be Reminded of:
If we do not provide a prototype declaration of the function in a situation
where it is defined before it gets called, then th eprogram works fine.
However, on the other hand, if the prototype declaration is not present and
we call the function at a point before it is defined, then we get an error.

void fun() int main() void fun(); // declaration


{ { int main()
------ fun(); //error {
} fun(); //fine, no error
} void fun() }
int main() { void fun()
{ ------ {
fun(); //fine, no error ------
} }
}

Harapriya Mohanta
Function calling
• In order to use a function, we have to call it, i.e., function calling. In a program, when we call
a function, the program’s control is transferred to that function and execute the code(body)
of the function.
• To call a function, we simply need to pass the required arguments along with the function name.
• for ex:
add(10,20); #include<stdio.h>
add(x,y); void fun(); //function prototype
int main()
{ o/p:
printf(“i am from main function\n”); i am from main function
fun(); //function calling i am from userdefined function
return 0;
}
// function definition
void fun()
{
printf(“i am from userdefined function\n”);
}
Different ways (types) to define a function:

1. Function with no return-type and no arguments. e.g., void fun()


2. function with return-type and no arguments. e.g., int fun()
3. function with no return-type and with arguments. e.g., void fun(int a, int b)
4. function with retirn-type and arguments. e.g., int fun(int a , int b)

Harapriya Mohanta
Formal parameters & Actual Parameters
• The aparametrs used in function prototype and definition are knwon as the formal
arguments or parameters.
• The parameters that are used in the function calling are knwon as the actual arguments.

Formal arguments
void add(int a, int b)
{
printf(“sum:%d”,(a+b));
}
int main()
{
int x =10, y =20;
add(x,y);
Actual arguments
return 0;
}
Harapriya Mohanta
Argument/parameter passing to the function

There are 2 ways to pass the parameters to the function.


1. pass by value or call by value
2. pass by address or call by address

Pass by Value or call by value:


When the argument(variable ) to a function is passed by value, then only a copy of the values of
the actual arguments is passed into the function and any change or calculation performed by the
function on the arguments does not affect the original data of the arguments.
Pass by address or call by address:
When the argument(variable ) to a function is passed by address, then the address of the actual
arguments is passed into the function and any change or calculation performed by the function
on the arguments change the original data of the arguments.

Harapriya Mohanta
Call by Value Call by address

int swap(int x, int y) int swap(int *x, int *y)


{ {
int t; int t;
t= x; t= *x;
x=y; *x=*y;
y=t; *y=t;
} }
int main() int main()
{ {
int a =10, b=20; int a =10, b=20;
printf(“before swapping: %d %d”,a,b); printf(“before swapping: %d %d”,a,b);
swap(a,b); swap(&a, &b);
printf(“after swapping: %d %d”,a,b); printf(“after swapping: %d %d”,a,b);
} }

o/p: before swapping:10 20 o/p: before swapping:10 20


after swapping:10 20 after swapping:20 10
Harapriya Mohanta
Harapriya Mohanta

You might also like