Function in c programming language

Function in C programming language with example

Social Shares
  • 1
  •  
  •  
  • 1
  •  
  •  
  •  
  •  

Hello readers, welcome to our blog. In the last article, you had learnt about the Strings In C Programming Language. I hope you all understood how to declare a string in c, how to use string and gets() puts() functions but in today’s article, you will learn how to declare a function in c programming language.  So let’s get started.


Topics that we cover in function in c programming language

  1. Function definition.
  2. Advantages Of Function.
  3. Types of functions.
    1. Pre-Defined function.
    2. User-Defined function.
      1. Function Declaration.
      2. ƒunction Definition.
      3. Function Call.
  4. Functions can be called by 2 type in C language.
    1. Call By Value.
    2. Call By Reference.

Function in C programming language

Many times your program may have some such statements that require you to execute the compiler again and again. In such a situation, it may take a lot of time to write these statements over and over again. Additionally, the program becomes very big, which makes it difficult to read and understand.

To avoid this situation, C provides a mechanism that is called a function. Any statement that you want to execute many places in the program repeatedly writes them in a block and gives this block a unique name.

After this, every time you want to execute these statements in the program, you call that function through that unique name of the function. All statements are written in the same function as the call are executed.

The name and parameters of the Function (you will read more about them) when you type it is called function declaration. When you type all the statements being executed in the function, it is called function definition. When you use any of the functions in the entire program, it is called a function call.

The advantages of using the function in c programming language:-

  1. Creating functions creates the programmer’s time and memory of the computer.
  2. The same code can be easily used repeatedly. This increases the code re-usability.
  3. Program module divides into it so that it can easily be managed and debug
  4. The readability of the program increases.

There are two types of Function in C programming language.

  1. Predefined Functions
  2. User-defined Functions

These two types of functions are being explained in detail.

Predefined Functions

Predefined Function in C programming language are those functions which are already provided in the C library. These functions have already been declared and defined. Simply have to include header files to use them.

For example, if you want to use functions such as scanf () and printf () in your program, then you include <stdio> header file in your program. These are both predefined function in c programming language.

Some examples of predefined functions are given below.

  • scanf()
  • printf()
  • strcpy()
  • void *malloc()
  • int to lower() etc…

User Defined Functions

User-defined functions are those functions which programmer (you) create yourself. A programmer can create as many functions as per your requirement. How these functions are created and how they are used. All information about this is being given below.

predefined function vs userdefined function

Creating and Using Function in C programming language

It’s very easy to create and use functions in C. This process has 3 main steps.

Each of these steps has been explained further. Let’s try to know about them.

Function Declaration

In this part, you define the name of the function, its return type and the parameters. The general syntax of the Function declaration is given below.
<return-type> <function-name>(<list-of-parameters>);
  • Return-type – What kind of value will return when your function execution is complete, you define it by return type. For example, if you are creating a program of addition which adds 2 whole numbers, then your return type is int.
  • Function-name – This is the name of your function. This should be unique in the program. When you call the function, you write this name only.
  • List-of-parameters – This is the list of the variables that you will pass when calling the function. For example, if you are creating the addition of function then you can pass 2 numbers or 2 variables as parameters, and then add them inside the function and show results. It is not necessary that you define parameters in all functions.
One thing you should always remember is that the function declaration statement is terminated from a semicolon. But this does not happen with the function definition. Suppose that you are making the addition function which adds two numbers, then you declare it like this.
int add(int a, int b);

Function Definition

This part is also called the function body. In this, you write the statements that you want to execute. The general syntax of the Function definition is given below.
<return-type> <function-name>(<list-of-parameters>)
.{
      štatement 1;
      statement 2;
       .
       .
      statement N;
}

Function definition includes return-type, function-name, and list-of-parameters in the same way as in function declaration. These are followed by statements written in the block of curly brackets that you want to execute.

For example, if you are creating an addition function, then you can type the definition as follows.
int add(int a, int b)
{
    int c;
    c = a+b;
    return c;
}

 

Function Call

Whenever you want to use the function in the program, you call it. The general syntax of the function call is being given below.
<function-name>(<arguments-list>);

argument-list – arguments are those real values that you pass on when calling functions. These values are assigned to parameters in the function definition. After this, the values are processed and the result is returned.

For example, when you call the addition function, you will pass 2 values such as 2 and 7, instead, they can be passed in variables, which have values stored.

These values will be assigned to variable variables a and b and are processed on these variables within the function. Functions that have parameters defined and if you do not pass arguments when doing a function call then the program gets an error.

For example, if you created the addition of a function, you can call it like this.

add(2,7);

Functions can be called by 2 type in C language.

These two types of function calls will be explained with further examples.

Call by Value

When you pass a value in the form of an argument while calling the function, it gets copied into the parameter variables and after that, the operations are performed on those variables.

Similarly, when you pass a variable while calling the function, in fact, it is not passed in the variable function, but that variable is copied to the value parameter variable and then performing operations on that parameter variable.

For example, if two integer variables x and y pass when calling the addition function of the above given above, then the value of those variables will be copied into the variable variables a and b and returning the results by doing the addition to them. Will go.

Τhis type of function call is called call by value. Īt does not change the value variable of the variable variables, and whatever operation happens it performs only on the parameter variables. Ân example of this is being given below.

#include<stdio.h>int add(int a, int b);

int main()
{
int result;
int x=5;
int y=4;

printf(“This addition is performed using function call by value\n”);

result = add(x,y); //Call by value

printf(“Result is : %d”,result);
}

int add(int a, int b)
{
int c;
c=a+b;
return c;
}

The above example produces the below-given output.
This addition is performed using a function call by value
Result is 9

 

Call by Reference

When calling a function in c programming language, instead of passing the actual variables, their address can also be passed. By doing so, the addresses in those variables will be copied into variables and the parameter variable will actually point to the argument variables.

This type of function call is called call by reference. In this type of function call, the address of the actual argument variables is passed. By doing so, if changes in the values of parameter variables in the function, the changes in the values of the argument variables also occur. I mean changes in arguments also change in arguments.

In this type of function call, arguments are passed along with the address of operator (&). It passes the address parameter variables of the operator argument. Apart from this, the parameters in the declaration and definition of the function are defined with the value at (*) operator.

An example of the function call by reference is being given below.

#include<stdio.h>int add(int *s);  //Declaring add function with value at operator

int main()
{
int s=2;

printf(“Call by Reference Demo\n”);

add(&s); //Calling add() function by reference

printf(“Sum is %d\n”,s);
}

int add(int *s) //defining add function with
{
*s = *s+2;
return 0;
}

The above example produces the below-given output.
Call by Reference Demo
Sum is 4

Example of C Functions

A simple example of using the Function in C programming language is being given below.
#include<stdio.h>
int add(int a, int b); /* function declaration */
int main()
{
   int result;
    printf(“This is  addition program using function!\n”);
    result = add(2,7);  /* function call */
    printf(“Result is : %d”, result);
 }
int add(int a, int b)  /* function definition */
{
     int c;
     c = a + b;
     return c;
}
The above program produces the below-given output.
This is an addition program using the function!
The result is: 9

Social Shares
  • 1
  •  
  •  
  • 1
  •  
  •  
  •  
  •  

One comment

Leave a Reply

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