C Function

by anupmaurya

In this tutorial, you’ll learn about C Function, types of function, why use function and Passing value to functions.

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.

What is C 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.

or we can say that, Function is a part of program that invoked by another part of a program.

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.

Key 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.

Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.