Functions in C programming

 Functions in C programming

Here you will learn about functions and how the functions works and few example related to functions in c programming.




Functions are set of statements that performs some specific tasks. So we can divide a larger problem instance into smaller functions. Functions are written only once and can be used how many times as needed by a programmer. So functions help to reduced the work by allowing to write a piece of code once and not repeating that code many times, where ever appropriate call the function. The main() function that should be present in a c program is a function and main() is a starting point of a program where program starts executing.


For example if u want to do sum of two numbers 10 times u don't want to compute the sum for 10 times there we can use functions.

There are mainly two types of functions:-

  • Standard Library Functions 
  • User defined Functions

Standard Library Functions :- 

  • These are the built-in functions that are already defined header files of the library.
  • Example printf() and scanf() ...etc.

User defined Functions:-

These are the user-defined functions as  name itself suggests it is defined by the user themselves and use in the program.

A function in c consists of function prototype that tells the compiler about the return-type, number and sequence of parameters and function definition that contains the group of statements that is executed when the function is called or invoked.

Function prototype:- 

It can be defined either with variable name in the parameters or can also be defined only with the datatypes as shown below.

Syntax:-

 return_type function_name(arg1, arg2, ....);


Example:- After the header section we specify the functions prototype

int add(int, int);
or
int add(int a, int b);


 Function definition:-

Syntax:- 

return_type function_name(arg1, arg2, ....) 

{

    function_body;
}


Where,

return_type:- specifies the return data type of the function like int, float or void ..etc.

function_name:- specifies the name of the function which is used to invoked the function.

Parameters:- specifies the list of parameters like data type, the number of parameters passed to the function.

function body:- where the  statements are present which defines the what the function does and is written a pair of braces.

How functions works:-

  • When the functions is prototype is defined the compiler knows the return type, list of arguments and the function name. 
  • After the function prototype is defined write the function definition.
  • When the function is called the control will go to the function definition and starts executing it.
  • After the execution finished the control return back to the statement after the function call statement.

Example program to add two numbers using functions:-

In this program we are using a function  add two integer numbers and we have defined the prototype of the function after the header and defined the function after the main function. In the main function we are assigning the sum variable to a function call as shown below in the program and the value returned is stored in variable sum. The function add() take two parameters of integer type and it takes the value which we are passing and storing it in num1 and num2 variable and the compute the sum and returns.


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

int main()
{
    int a, b, sum=0;
    a=10, b=20;
    sum = add(a, b);
    printf("Sum: %d", sum);

    return 0;
}

int add(int num1, int num2){
    return num1+num2;
}

Output:-
Sum: 30
We can also write the function without the function prototype but in this case we define the function after the header files.

Example:-


#include <stdio.h>

int add(int num1, int num2){
    return num1+num2;
}

int main()
{
    int a, b, sum=0;
    a=10, b=20;
    sum = add(a, b);
    printf("Sum: %d", sum);

    return 0;
}

Output:-
Sum: 30
As you can see we got the same output.

Post a Comment

0 Comments