C - Categories of Functions - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about categories of functions.
All the C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Depending on the arguments and return values functions are classified into 4 categories:

Function without arguments and without a return value.
Function with arguments and without a return value.
Function without arguments and with a return value.
Function with arguments and with a return value.

Function without arguments and without a return value.
Function with arguments and without a return value.
Function without arguments and with a return value.
Function with arguments and with a return value.
When a function has no arguments, it does not receive any data from the calling function. similarly, when a function does not return a value, the calling function does not receive any data from the called function.
In effect, there is no data transfer between the calling function and the called function in the category function without arguments and without a return value.
Let’s consider an example of a function without arguments and without a return value:
#include <stdio.h>
void country_capital(void);
void main() {
  country_capital();
}
void country_capital() {
  printf("New Delhi is the capital of India\n");
}
Output:
New Delhi is the capital of India
In the above sample code the function
void country_capital(void); 
specifies that the function does not receive any arguments and does not return any value to the main() function.
When a function definition has arguments, it receives data from the calling function.
The actual arguments in the function call must correspond to the formal parameters in the function definition, i.e. the number of actual arguments must be the same as the number of formal parameters, and each actual argument must be of the same data type as its corresponding formal parameter.
The formal parameters must be valid variable names in the function definition and the actual arguments may be variable names, expressions, or constants in the function call.
The variables used in actual arguments must be assigned values before the function call is made. When a function call is made, copies of the values of actual arguments are passed to the called function.
What occurs inside the function will have no effect on the variables used in the actual argument list. There may be several different calls to the same function from various places with a program.
Let’s consider an example of a function with arguments and without return value:
#include <stdio.h>
void largest(int, int);
void main() {
  int p, q;
  printf("Enter two numbers : ");
  scanf("%d%d" , &p, &q);
  largest(p, q);
}
void largest(int x, int y) {
  if (x > y) {
    printf("Largest element = %d\n", x);
  } else {
    printf("Largest element = %d\n", y);
  }
}
Output:
Enter two numbers : 1
10
Largest element = 10
In the above sample code the function
void largest(int, int);
specifies that the function receives two integer arguments from the calling function and does not return any value to the called function.
When the function call largest(p, q) is made in the main() function, the values of actual arguments p and q are copied into the formal parameters x and y.
After completion of execution of largest(int x, int y) function, it does not return any value to the main() function. Simply the control is transferred to the main() function.
When a function has no arguments, it does not receive any data from the calling function. When a function returns a value, the calling function receives data from the called function.
Let’s consider an example of a function without arguments and with a return value:
#include <stdio.h>
int sum(void);          
void main() {
  printf("\nSum of two given values = %d\n", sum());
}
int sum() {
  int a, b, total;   
  printf("Enter two numbers : ");
  scanf("%d%d", &a, &b);
  total = a + b;
  return total;       
}
Output:
Enter two numbers : 1
2
Sum of two given values = 3
 
In the above sample code the function
int sum(void);
specifies that the function does not receive any arguments but returns a value to the calling function.
When a function definition has arguments, it receives data from the calling function.
After taking some desired action, only one value will be returned from called function to the calling a function through the return statement.
If a function returns a value, the function call may appear in any expression, and the returned value used as an operand in the evaluation of the expression.
Let us consider an example of a function with arguments and with return value:
#include <stdio.h>
int largest(int, int, int);
void main() {
  int a, b, c;
  printf("Enter three numbers : ");
  scanf("%d%d%d" , &a, &b, &c);
  printf(" Largest of the given three numbers = %d\n", largest(a, b, c));
}
int largest(int x, int y, int z) {
  if ((x > y) && (x > z)) {
    return x; 
  } else if (y > z) {
    return y;
  } else {
    return z;
  }
}
Output:
Enter three numbers : 1
4
7
Largest of the given three numbers = 7

In the above sample code the function
int largest(int, int, int);
specifies that the function receives three values and returns a value to the calling function.
crasseux – Functions
C Functions
User defined functions in C
Happy Learning 🙂

C How to Pass Arrays to Functions
Functions in C
User defined Functions in C
PHP Functions Example Tutorials
Python Decorators – Classes and Functions
Dynamic Memory Allocation in C
JQuery Callback Functions Example Tutorial
PHP String functions Example Tutorials
C Program – Find Largest and Smallest number in an Array
Understanding the structure of a C Program
C – Derived and User Defined Data Types
Java Program To Find Largest and Smallest Number in Array
Java Program to find Smallest and Largest element in Array
C – Header Files & Preprocessor Directives
Understanding Scope & Lifetime of Variables

C How to Pass Arrays to Functions
Functions in C
User defined Functions in C
PHP Functions Example Tutorials
Python Decorators – Classes and Functions
Dynamic Memory Allocation in C
JQuery Callback Functions Example Tutorial
PHP String functions Example Tutorials
C Program – Find Largest and Smallest number in an Array
Understanding the structure of a C Program
C – Derived and User Defined Data Types
Java Program To Find Largest and Smallest Number in Array
Java Program to find Smallest and Largest element in Array
C – Header Files & Preprocessor Directives
Understanding Scope & Lifetime of Variables
 


Δ
 C – Introduction
 C – Features
 C – Variables & Keywords
 C – Program Structure
 C – Comment Lines  & Tokens
 C – Number System
 C – Local and Global Variables
 C – Scope & Lifetime of Variables
 C – Data Types
 C – Integer Data Types
 C – Floating Data Types
 C – Derived, Defined Data Types
 C – Type Conversions
 C – Arithmetic Operators
 C – Bitwise Operators
 C – Logical Operators
 C –  Comma and sizeof Operators
 C – Operator Precedence and Associativity
 C – Relational Operators
 C Flow Control – if, if-else, nested if-else, if-else-if
 C – Switch Case
 C Iterative – for, while, dowhile loops
 C Unconditional – break, continue, goto statements
 C – Expressions and Statements
 C – Header Files & Preprocessor Directives
 C – One Dimensional Arrays
 C – Multi Dimensional Arrays
 C – Pointers Basics
 C – Pointers with Arrays
 C – Functions
 C – How to Pass Arrays to Functions
 C – Categories of Functions
 C – User defined Functions
 C – Formal and Actual Arguments
 C – Recursion functions
 C – Structures Part -1
 C – Structures Part -2
 C – Unions
 C – File Handling
 C – File Operations
 C – Dynamic Memory Allocation
 C Program – Fibonacci Series
 C Program – Prime or Not
 C Program – Factorial of Number
 C Program – Even or Odd
 C Program – Sum of digits till Single Digit
 C Program – Sum of digits
 C Program – Reverse of a number
 C Program – Armstrong Numbers
 C Program – Print prime Numbers
 C Program – GCD of two Numbers
 C Program – Number Palindrome or Not
 C Program – Find Largest and Smallest number in an Array
 C Program – Add elements of an Array
 C Program – Addition of Matrices
 C Program – Multiplication of Matrices
 C Program – Reverse of an Array
 C Program – Bubble Sort
 C Program – Add and Sub without using + –