×
>
<

Aptitude

Functions

Function 

A function in C is a block of code which is used to perform specific tasks as many times as it is called, and is used to make coding simpler and convenient using a group of statement and operations.

C function, this is a set of statements. there is a function in every single program. where function is required, function is called.

Note: int main() is also a function, every code has at least one function.

C Functions

Advantages of function:-

  • The code written in the function does not have to be written repeatedly.
  • function protects programmer’s time and program space.
  • Large program can be divided into small functions.
  • The function can be called repeatedly where needed.
  • The readability of the program increases.
Function Types

Predefined Function

  • In-built functions are also called predefined or Library functions.
  •  In-built functions, different header files or processors have been created for each functions.
  • There are lots of header files in C and they are grouped by different functions.
  • If programmer wants to create your own header files too.
  • Function declaration and definition is in header files.

     Example:

  • printf()
  • scanf()
  • strcpy()

User-defined function

User defined functions are those functions which programmer (you) create for yourself. Programmer can create as many functions as per your requirement.

Declaration of a function

When writing a function we need to tell the computer the name, the return type and the parameter(if any) of that function.

return-type – What kind of value will return when your function execution is complete, you define it by return type. If you are creating an addition program which adds 2 whole numbers then your return type is int.

function-name – this is the name of your function. These should be unique in the whole program. When you call the function, you write this name only.

list-of-parameters – These are the lists of variables that you will pass when calling the function. For example, if you are creating 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 semicolon. But this does not happen with the function definition.

Function Definition

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

Let’s see how

  
#include "stdio.h"
int add(int a,int b)  //a,b are the parameters which can be provided while calling the function
{
  int c;
  c=a+b;
  return c;//returns the integer value of the sum here
}
  
Function calling in C

1. Call by Value:

When single or multiple values of an actual argument are copied into the formal parameter of a function, the method is called the Call by Value. Hence, it does not alter the function's actual parameter using the formal parameter.

2. Call by Address:

In this method, the address of the actual argument is copied into the function call's formal parameter; the method is known as Call by Address. If we make some changes in the formal parameters, it shows the effect in the value of the actual parameter as it indirectly refers to the address of actual parameter. So It access the actual value address and changes it !

1. Call by Value :

Example of Call by Value :

  
#include <stdio.h>  
int main()  
{  
    int x = 10, y = 20;  
    printf (" x = %d, y = %d from main before calling the function", x, y);  
    CallByValue(x, y);  
    printf( "\n x = %d, y = %d from main after calling the function", x, y);  
}  
int CallByValue( int x, int y)  
{  
    x = x + 5;  
    y = y + 5;  
    printf (" \nx = %d, y = %d from modular function", x, y);  
}   
  

Output :

  
x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
x = 10, y = 20 from main after calling the function
  
2. Call by Address :

Example of Call by Address :

  
#include <stdio.h>  
int main()  
{  
    int x = 10, y = 20;  
    printf (" x = %d, y = %d from main before calling the function", x, y);  
    CallByAdd (&x, &y);  
    printf( "\n x = %d, y = %d from main after calling the function", x, y);  
}  
int CallByAdd( int *a, int *b)  
{  
    *a = *a + 5;  
    *b = *b + 5;  
    printf (" \nx = %d, y = %d from modular function", *a, *b);  
} 
  

Output :

  
x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
x = 15, y = 25 from main after calling the function