×

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

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 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.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.