Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Wednesday, January 24, 2018

User defined Function in C programming

A function is a self-contained block of statements which we can use whenever we want. A user-defined function is a type of function which is defined by the user.


Types of user-defined function in C
  • Function with no arguments and no return value.
  • Function with arguments and no return value.
  • Function with no arguments and a return value.
  • Function with arguments and a return value.

Type 1: Function with no arguments and no return value.

// example of a void function with no arguments.

#include <stdio.h>

void Add()
{
    int a, b;               // declaring variables.
    scanf("%d %d",&a, &b);  // taking input from user.
    
    int sum = a + b;        // storing the summation of given two numbers.
    printf("Summation of given two numbers = %d \n", sum);
}


int main()
{
    Add(); // calling function from the main.
    return 0;
}

Note
  • Above Function has no arguments and no return value. So it is a void function.
  • After calling this function from the main, this function will take input from the user and print the sum of two numbers.

Type 2: Function with arguments and no return value.

// This is an example of a void function with arguments.

#include <stdio.h>

void Add(int a, int b)
{
    int sum = a + b;        // storing the summation of given two numbers.
    printf("Summation of given two numbers = %d \n", sum);
}


int main()
{
    int x, y;               // declaring variables.
    scanf("%d %d",&x, &y);  // taking input from user.

    Add(x, y); // calling function from the main.

    return 0;
}

Note
  • Above Function has arguments but no return value. So it is also a void function.
  • Above function is passing parameters from the main after that, Add function will print the summation of these two numbers.

Type 3: Function with no arguments and a return value.

// This is an example of a no argument function which has a return value.

#include <stdio.h>

int Add()
{
    int a, b;               // declaring variables.
    scanf("%d %d",&a, &b);  // taking input from user.

    int sum = a + b;        // storing the summation of given two numbers.
    return sum;             // return the sum value
}


int main()
{
    int result = Add();  // calling function from the main and receiving the return value.

    printf("Summation of given two numbers = %d \n", result);
    return 0;
}
Note
  • When we call the function from the main it will return a value.
  • For receiving the return value we used result variable and assigned the Add() function into that variable so that the return data can be stored into the variable called result.
  • We can use different names for the variable but the datatype of the must be same as the function return data type.

Type 4: Function with arguments and a return value.

// This is an example of a function which has arguments and a return value also.

#include <stdio.h>

int Add(int a, int b)
{
    int sum = a + b;        // storing the summation of given two numbers.
    return sum;             // return the sum value
}


int main()
{
    int x, y;                // declaring variables.
    scanf("%d %d",&x, &y);   // taking input from user.

    int result = Add(x, y);  // calling function from the main.

    printf("Summation of given two numbers = %d \n", result);
    return 0;
}
Note
  • In this case we take input into the main function and passed it through the Add() function which is the only difference from the previous example.


Previous Topic

Next Topic



Structure of a Function in C programming

At that point we should have a good idea on C programming so it is time to move on a topic like Function. What is function? It is little bit difficult to explain in a sentence. We know what is loop and what it does, right? We used loop to execute any block of code several times but what if I want to repeat any block of code at any time/part of our program! Exactly, we can do this using function.


Function is a self contained block of statements which we can use whenever we want. Every Program has at least one function in C programming called ‘main()’.


Structure of a function
return_type  function_name ( parameter_list )
{
     // Function body
}

return_type : return_type is the date type which any function returns in C. Function in C must have a return type.

Functions may or may not return any value. If it does not returns any value then the return_type will be void. void function indicates it returns nothing.

function_name : function_name is simply the name of the function.

parameter_list : Parameters are like placeholder. Any function may have zero or more parameters. Any types of parameters can be used in a function. If there are more than one parameters then every parameter will be separated by a comma.

Types of Function in C
  • Standard Library Functions ( ex. scanf(), printf(), etc )
  • User Defined Functions

Look at the following example for better understanding

#include <stdio.h>

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

int main()
{
    int a, b;
    scanf("%d %d",&a, &b);
    
    int sum = add(a, b);   // calling function from main.
    printf("Summation of given two number is %d \n", sum);

    return 0;
}


Note
  • Above function has a return value of integer.
  • return keyword is used in C to return value from function.
  • To use any function in C programming, it must be called from the main function otherwise it won't work.

Advantages of using Function in C
  • We can reuse our code at anywhere in our program.
  • It makes debugging and editing more easier.
  • It makes program more readable.
  • It makes code more optimized.


Previous Topic

Next Topic