Everything you need to know about Function in C Programming [with video]

Functions are awesome facility of C programming language. 

What is function?

Functions are nothing but the block of codes which performs certain tasks. Every C program has at least one function, main(). We have two types of functions in C. 1. Built-in functions 2. User Defined functions. 
Built-in functions are predefined in C and we can use it by calling its name like: printf(), scanf(), getch() etc. They can perform their task accordingly. But in case of user defined functions, we have to declare, define and call them as per our requirements.

Why we need functions? 

==> As you know Unix Operating System was written in C. It is also used in embed and system programming. So, those tasks are large. Writing those all codes inside a single program will be very buggy and heavy as well. If we encounter any error on that program, it might take several days-weeks. (kidding). As well as one person can't develop those big things. So, we need teams. 
 
So, to minimize that thing, in Procedural programming, we  can use functions. In functions, we can divide those large programs to smaller modules and work separately which will divide tasks to different teams and after developing all the modules we can integrate to main program finally by the functions. 

Beside that, if we need to perform certain task again and again for that we can create a function and use that by calling it again and again. You don't have to write many lines of codes rather use functions and minimize the codes and tasks as well. 

Alright then, how can we create a function in C programming ?

For that, we have to know some components of functions like : Function Definition, Function Declaration or prototype, Calling function, Return statement, Function parameter (arguments etc)

Let's look at this code now.
#include<stdio.h>
void printMyName(); //Declaration of function
int main(){
printMyName();   // Calling function
return 0;
}
//Function Definition
void printMyName(){
printf("My Name is TechLover");
}

As you can see, we have created a small function named printMyName() which prints the "My Name is TechLover" to the screen. Also, there you can see we have commented with double slash(//) to let you know, what the particular area is. 

Category of functions

Type 1. : No return values but with parameter
#include<stdio.h>
void sum(int, int); //Declaration of function

int main(){
sum(5,10);   // Calling function
return 0;
}
//Function Definition
void sum(int a, int b){
int sum;
sum=a+b;
printf("The summation is %d ",sum);
}

Here we have passed 2 parameters of integer type (i.e. 5,10) to function sum. Since the function is type of void, it doesn't return value to main function.

Type 2 : With Return Values and Parameters
#include<stdio.h>
int greater(int, int); //Declaration of function

int main(){
int greater1,greater2,greater3;
greater1=greater(50,10);   // Calling function
printf("The greater value is %d \n",greater1);
printf("The greater value is %d \n",greater2);
printf("The greater value is %d \n",greater3);
return 0;
}

//Function Definition
int greater(int a, int b){
    if(a>b){
    return a;
    }
    else{
    return b;        
    }
}

Here we have declared a function with return type integer and we have passed two integer parameters (arguments as well). The return statement used inside the function greater() will return the greater number between a & b to the main program and main program will print that. 

Type 3 : No return type, no parameters #include<stdio.h>
void printMyName(); //Declaration of function
 
int main(){

printMyName();   // Calling function
 
return 0;
}

//Function Definition
void printMyName(){
printf("My Name is TechLover");
}

The above function have void return type, which doesn't returns anything to main program as well as it doesn't have parameters as well.

Type 4: With return values but no parameter 
#include<stdio.h>
int sum(int, int); //Declaration of function

int main(){
int result;
result=sum(3,2);   // Calling function
printf("The summation is %d",result);
return 0;
}

//Function Definition
int sum(int a, int b){
int sum;
printf("Enter two numbers :");
scanf("%d %d",&a,&b);
sum=a+b;
return sum;
}

In the above program, type of function is integer, so the function will return an integer value to main function but while calling the function, main function doesn't pass any argument or parameter.

So, those were the different categories of functions. 

Different types of functions calls in C

There are two types of function calls in C programming.
1. Function call by value (or Pass arguements by value)
2. Function call by Reference (Pass arguements by address)
 
1. Function call by value (or Pass arguements by value)
 
#include<stdio.h>
int sum(int, int); //Declaration of function

int main(){
int result;
result=sum(3,2);   // Calling function
printf("The summation is %d",result);
return 0;
}

//Function Definition
int sum(int a, int b){
int sum;
printf("Enter two numbers :");
scanf("%d %d",&a,&b);
sum=a+b;
return sum;
}
In the code above, when we are calling the function from the main program, we have passed to parameters (i.e. 3,2) to sum function, which is called function call by value because we have passed certain values to the called function when we call it.

2. Function call by reference
#include<stdio.h>
int sum(int *, int *); //Declaration of function

int main(){
int a=3,b=2,result;
result=sum(&a,&b);   // Calling function
printf("The summation is %d",result);
return 0;
}

//Function Definition
int sum(int *x, int *y){
int sum;
sum=*x+*y;
return sum;
}
 
In the code above, we have used *, & operators which are pointer operators. 
By using pointer variables we have received the value which is passed by the main program when calling the function sum. When we use * operator with variable name, it will create a pointer variable which can hold the memory address of particular variable. and & operator is used to pass the memory address of the variable which is pointed by that particular variable. 
So, this type of function call, where we pass the memory address instead of value, is called function call by reference. 
 
 I hope you have understood the function, it's type, benefits of functions, calling procedures and everything regarding functions in C. Still, if you have any confusion you can see the video below, where we have shown practical demonstration of all these programs and try to explain in easy to understand manner. 

Video Link : here

So, if you have any queries, comments, feedback then you can shoot them in our comment section or contact.techlover@gmail.com as well. 

This is it for now. 
 
staySafe! stayPositive!
 
Thank you.





Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment