Function Call

 

Functions in C Programming

Why we need functions in C

a) To improve the readability of code.

b) Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.

c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.

d) Reduces the size of the code, duplicate set of statements are replaced by function calls.

A function is a block of statements that performs a specific task.

Syntax of a function

return type function name (argument list)

{

    Set of statements – Block of code

}

Defining a Function in C

The function definition, in general, holds this form in the C programming language:

 

 

return_type name_of_function( parameter list )

 {

Function body

}

Function Name − It denotes the actual name of any function that we are using in the given code. The name and parameter of a function constitute a function signature in the code together.

Function Body − It consists of a collection of all the statements. These provide a definition of what the function will do in the code.

Parameters − It is just like a placeholder. Whenever we invoke a function, we pass a value to the available parameter. We refer to this value as an argument or an actual parameter. We refer to the parameter list as the number, order, and type of the function in the code. The parameters may be optional. It means that any function in a code may consist of no parameters at all.

Return Type − we may get a value in return for a function. The term return type refers to the data type of the returns that we get from the functions (function returns). Some of the functions are also capable of performing any desired operation without getting a value in return for it. In any such case, the keyword used for the return type will be void.

 

Function Call

When a function gets called by a program, the called function will get the program control transferred to it. The called function will be performing a defined task.

When a function gets called by a program, the called function will get the program control transferred to it. The called function will be performing a defined task.

// call and definition

#include <stdio.h>

 // Function that takes two parameters

// a and b as inputs and return their sum

int sum(int a, int b)

{

  return a + b;

}

// Driver code

int main()

{

  // Calling sum function and

  // storing its value in add variable

  int add = sum(10, 30);

  

  printf("Sum is: %d", add);

  return 0;

}

Function Arguments

When a function needs to use the arguments, it has to declare those variables that happen to accept the arguments’ values. Such variables are known as the given function’s formal parameters.

Call By Reference-The Call by Reference method creates a copy of the address of the given argument into the parameter that is formal in nature. Inside this function, the use of address helps in accessing the actual argument that comes in use in this call. It means that the changes that appear on the parameter are bound to affect the given argument.

Call By Value-The Call by Value method creates a copy of the actual value of the given argument in the parameter of the function that is formal in nature. Here, the changes that appear on the parameter (that exists inside the function) create no effect whatsoever on the available argument.

 

 

 

 

 

 

 

 

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

4.Time Management

                                      Time Management   •        Effective time management in project management involves strategic plann...