Function in c programming

Sometimes our program gets bigger in size, and its not possible for a programmer to track which piece of code is doing what.

The function is a way to break our code into chunks so that it is possible for a programmer to reuse them.

function is a block of code that performs a particular task.

There are many situations where we might need to write same line of code for more than once in a program. This may lead to unnecessary repetition of code, bugs and even becomes boring for the programmer. So, C language provides an approach in which you can declare and define a group of statements once in the form of a function and it can be called and used whenever required.

These functions defined by the user are also know as User-defined Functions

C functions can be classified into two categories,

  1. Library functions
  2. User-defined functions

Library functions are those functions which are already defined in C library, example printf()scanf()strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.

User-defined functions on the other hand, are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space.

What is a function?

A function is a block of code that performs a particular task. A function can be reused by the programmer in a given program any number of times.

Example and syntax of a function:

#include<stdio.h>
void  display();                   // Function prototype
int main(){
int a;
display();                          // Function call
return(0);
}

void display(){                           // Function definition
printf(“Hi I am display”);
}

Function prototype:

Function prototype is a way to tell the compiler about the function we are going to define in the program.

Here void indicates that the function returns nothing.

Function call:

Function call is a way to tell the compiler to execute the function body at the time the call is made.

Note that the program execution starts from the main function in the sequence the instructions are written.

Function definition:

This part contains the exact set of instructions that are executed during the function call. When a function is called from main(), the main function falls asleep and gets temporarily suspended. During this time, the control goes to the function being called when the function body is done executing main() resumes.

Quick Quiz: Write a program with three functions,

  1. Good morning function which prints “Good Morning.”
  2. Good afternoon function which prints “Good Afternoon.”
  3. Good night function, which prints “Good night.”

main() should call all of these in order 1 – 2 – 3.

Important Points:

  • Execution of a c program starts from main()
  • A c program can have more than one function
  • Every function gets called directly or indirectly from main()
  • There are two types of functions in c. Let’s talk about them.
Types of Functions:
  • Library functions: Commonly required functions grouped together in a library file on disk.
  • User-defined functions: These are the functions declared and defined by the user.
Why use functions?
  • To avoid rewriting the same logic again and again
  • To keep track of what we are doing in a program
  • To test and check logic independently
Passing values to functions:

We can pass values to a function and can get a value in return from a function

int sum(int a, int b)

The above prototype means that sum is a function which takes values a(of type int) and b(of type int) and returns a value of type int

Function definition of sum can be:

int   sum(int a, int b){

int c;                                      => a and b are parameters

c=a+b;

return c;

}

Now we can call sum(2,3) [here 2 and 3 are arguments]; from main to get 5 in return.

int d=sum(2,3);   => d becomes 5

Note:

1. Parameters are the values or variable placeholders in the function definition. Ex: a & b

2. Arguments are the actual values passed to the function to make a call. Ex: 2 & 3

3. A function can return only one value at a time.

4. If the passed variable is changed inside the function, the function call doesn’t change the value in the calling function.

int change(int a){

a=77;                         => Misnomer

return 0;

}

change is a function which changes a to 77. No, if we call it from main like this.

int b=22;

change(b);                            => The value of b remains 22

printf(“b is %d”,b);                => prints “b is 22”

This happens because a copy of b is paused to the change function.

Advantage of functions in C

There are the following advantages of C 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 achievement of C functions.
  • However, Function calling is always a overhead in a C program.

Quick Quiz: Use the library function to calculate the area of a square with side a.

Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *