In this tutorial, we will learn about Functions in C. While writing programs there are many situations when we need to perform the same kind functionality many times. So it means we have to write the same code multiple times in the program. To overcome this kind of problem we have concept called functions in C. Functions help us to divide a big program into the number of small blocks and we can use these small blocks any number of times in program by just writing their names. Before going further must check out some advantages to use functions in C.
Page Contents
Advantages to using Functions in C:
- We can write a piece of code in a function and we can call it in the program several times so it helps us to avoid rewriting code again and again.
- We can call a function for any number of times.
- Functions make it so easy to manage program Because it easy to manage a small piece of code than a large piece of code.
Types of Functions:
Here we will learn what types of functions are available in C. There are a total of two types of functions available in C which are as follow:
- Library Function
- User-defined Function
Library Functions in C:
Library Functions are those which are already defines in header files of C such as scanf(), printf(), gets(), puts()etc.
User-defined Functions in C:
User-defined functions are those which programmers create in the program to perform some certain task. In this tutorial, we will learn about user-defined functions in detail.
Some important aspects of Functions in C:
There are two main aspects which you should keep in your mind while working with functions in C.
- Declaring a Function:
- Defining a Function:
- Calling a Function:
Declaring a Function:
The function declaration is used to tell the compiler about the return type of function, name of the function and the parameters of the function.
Syntax to declaring a function in C:
return_type function_name( parameter list );
Defining a Function:
Defining a function mean when we write the working of function. We write the code in function destination which will work when we will call the function.
Syntax to define a function in C:
return_type function_name( parameter list ) {
body of the function
}
- Return Type: A function can return a value. The return_type use to mention what kind of (data type) value a particular function will return. We can also write void keyword as return type which means function would not return something.
- Function Name: This is the name of the function. After defining a function we use this name to call function.
- Parameters: We can write formal parameter while defining a function. In case formal arguments are mentioned in function definition we have to pass actual arguments while calling that particular function.
- Function Body: Function body is where we mention actual working of function or in other words where we write code to perform some task.
Calling a Function:
After defining a function our next step is to call that function. The simple way to call a method in C just write the name of the function where you want to call it. To understand it please check out the following example program.
#include <stdio.h>
int main()
{
sayHello(); // calling to sayHello() function;
return 0;
}
//defining sayHello() function here
void sayHello(){
printf("Hello, Welcome to Owlbuddy");
}
In case you have a function with parameters there are two options to call it. which are as follow.
- First, one is Call by value
- Second, one is Call by reference
Call by Value:
In call by value technique passing arguments to function copy the value of actual parameters into its formal parameter. It means any change to value inside will not make any impact on arguments. Please check out the following program to understand it properly.
#include <stdio.h>
int main () {
int num1 = 10;
int num2 = 20;
printf("Before swap num1: %d\n", num1 );
printf("Before swap num2: %d\n", num2 );
//Calling to swap function
swap(num1, num2);
printf("After swap num1: %d\n", num1 );
printf("After swap num2: %d\n", num2 );
return 0;
}
void swap(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
return;
}
Output:
Before swap num1: 10
Before swap num2: 20
After swap num1: 10
After swap num2: 20
Call by Reference:
In call by value technique passing arguments to function copy the actual reference of parameters into its formal parameter. It means any change to value inside will make an impact on arguments. Please check out the following program to understand it properly.
#include <stdio.h>
//declaring function swap
void swap(int *x, int *y);
int main () {
int num1 = 10;
int num2 = 20;
printf("Before swap num1: %d\n", num1 );
printf("Before swap num2: %d\n", num2 );
/* calling a function to swap the values */
swap(&num1, &num2);
printf("After swap num1: %d\n", num1 );
printf("After swap num2: %d\n", num2 );
return 0;
}
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
Output:
Before swap num1: 10
Before swap num2: 20
After swap num1: 20
After swap num2: 10