×

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the C programming language, written functions may be declared with or without any arguments. When these functions are called for execution, they may or may not return any values as the return type is not expected to be void. Still, since there are no arguments passed by us while calling it from the primary program function, we haven't sent any argument values through the call we take here.

Functions in C

In layman's terms, a Function in C programming language is a block of code that will only run or which only executes when it is called from the primary main itself or any other secondary function.

Syntax of Function in C Programming Language

void this_is_my_Function() {
  // code to be executed
}
int main(){
this_is_my_Function()   //call the function in main program
return 0;
}

Code Example

//We are Creating a function
void myFunction() {
  printf("Function has just got executed!");
}


int main() {
  myFunction(); //We are call the function from here
  return 0;
}


// Outputs "Function has just got executed!"

Output

Function has just got executed!

C Function Argument and Return Values

1.Writing functions with no arguments and no return values

Syntax and Declarations

Declaring the functions : void function();
Calling the function : function();
Defining the function :
                      void function()
                      {
                        statements;
                      }

Code

//Writing down the C programming language code for
//function with arguments and no return values
#include <stdio.h>
void value(void);
void main()
{
    value();
}
void value(void)
{
    float year_here = 2, period_is_here = 2; 
    float amount_or_money = 52000, inrate_we_have_is = 0.188;
    float sum;
    sum = amount_or_money;
    while (year_here <= period_is_here) {
        sum = sum * (1 + inrate_we_have_is);
        year_here = year_here + 1;
    }
    printf(" The total money that we are having is %f:", sum);
}

Output

The total money that we are having is 61776.000000:

2. Writing functions with argument passing but without any return values

Syntax and Declarations

Declaring the functions : void function(int);
Calling the function : function(x);
Defining the function :
             void function(int x)
             {
               statements;}

Code Example

//Writing down the C programming code for the function
//with argument but without any return value
#include <stdio.h>
void function(int, int[], char[]);
int main()
{
    int abcd = 20;
    int arrr[5] = { 110, 11120, 330, 2240, 5110 };
    char strrr[30] = "javaTpoint.com";
    function(abcd, &arrr[0], &strrr[0]);
    return 0;
}
 
void function(int abcd, int* arrr, char* strrr)
{
    int i;
    printf("value of abcd we have is %d\n\n", abcd);
    for (i = 0; i < 5; i++) {
        printf("value of arrr[%d] is %d\n", i, arrr[i]);
    }
    printf("\nvalue of strrr is %s\n", strrr);
}

Output

value of abcd we have is 20


value of arrr[0] is 110
value of arrr[1] is 11120
value of arrr[2] is 330
value of arrr[3] is 2240
value of arrr[4] is 5110


value of strrr is javaTpoint.com

3. Functions which are having no arguments passed but returns a value

Syntax and Declarations

Declaring the functions : int function();
Calling the function  : function();
Defining the function  :
                 int function()
                 {
                     statements;
                      return x;
                  }

Code Example

//Writing down the C language code for function types  with no arguments
// but which can return value upon code execution
#include <math.h>
#include <stdio.h>
int sum_fun();
int main(){
    int number;
    number = sum_fun();
    printf("\nSum of two given values we have is = %d", number);
    return 0;
}
 
int sum_fun()
{
    int abcd = 23250, bcde = 2180, sum_fun;
    sum_fun = sqrt(abcd) + sqrt(bcde);
    return sum_fun;
}

Output

Sum of two given values we have is = 199

4. Functions which have arguments passed and can also return the values to the function call

Syntax and Declarations

Declaring the functions : int function ( int );
Calling the function : function( x );
Defining the function :
             int function( int x )
             {
               statements;
               return x;
             }

Code Example

#include <stdio.h>
#include <string.h>
int function(int, int[]);
int main(){
    int i_variable, abcd = 826328362839179191739191;
    int array[5] = { 29479,2727,88765,17173,1111111 };
    abcd = function(abcd, &array[0]);
    printf("value of abcd is %d\n", abcd);
    for (i_variable = 0; i_variable < 2; i_variable++) {
        printf("value of array[%d] is %d\n", i_variable, array[i_variable]);
    }
    return 0;
}
int function(int abcd, int* array)
{
    int i_variable;
    abcd = abcd + 20;
    array[0] = array[0] + 50;
    array[1] = array[1] + 50;
    return abcd;
}

Output

value of abcd is 1790010187
value of array[0] is 29529
value of array[1] is 2777

Related Topics

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.