Functions and Program Execution in C Program

This topic details functions and program execution in C.

Functions and Program Execution in C

What is a Function?

A function is a group or block of statements enclosed in flower brackets that can be executed independently upon calling.

A group of functions makes a program.

Every function must return a value.

#include <stdio.h>
int add(int x, int y)
{
return x + y;
}
int main()
{
int a, b, c;
printf("Enter two numbers: ");
scanf("%d %d", &b, &c);
a = add(b, c);
printf("Sum of two numbers = %d\n", a);
return 0;
}

Output

Enter two numbers: 10 20
Sum of two numbers = 30

The above example program, which is a user-defined function demonstrates the following,

ConceptExample Demonstration in the code
Parametersint x, int y
Function definition or Procedureint add(int x, int y)
Function calladd(b, c)
Return statementreturn x + y;
Return value stored in a variablea = add(b, c);

What are the Different Types of Functions?

Functions and Program Execution in C

There are two types of functions. One is the pre-defined functions and user-defined functions.

The user need not create the pre-defined functions as they are already defined inside the ‘C’ program, and the compiler knows them as they are already defined. One example of a pre-defined function is the main().

Another function is the user-defined function, where the user has to create the function.

How to Create a Function?

To create or declare a function, one has to follow three steps. That is, one, specify the function name along with the data type, two include parentheses (), and thirdly the curly or flower brackets {}.

int add(int x, int y)
{
return (x + y);
}

Functions help in Program Execution in C, and it involves following steps,

  • At first, the programmer needs to perform the calling function.
  • The called functions that include the function definition perform the logic.
  • After performing the logic, the called function returns the value.
  • In the final step, the control shifts to the calling or main function after the value reaches the closing bracket.

Leave a Comment