C function arguments and return values

C function argument and return values

All functions in C are known as either with arguments or without arguments.

1. Function with no argument, no return value

When a function with no arguments, does not receive any data from the calling function and when it does not return a value, the calling function does not receive any data from the called function.

 

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Function declaration : void function();
Function call : function();
Function definition :
void function()
{
statements;
}
Function declaration : void function(); Function call : function(); Function definition : void function() { statements; }
Function declaration : void function();
Function call : function();
Function definition :
                      void function()
                      {
                        statements;
                      }

Program function with no argument, no return value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
void value(void);
void main()
{
value();
}
void value(void)
{
int yr = 1, period = 5, amnt = 5000, inrate = 0.12;
float sum;
sum = amount;
while (yr <= period) {
sum = sum * (1 + inrate);
year = year + 1;
}
printf(" The total amnt is %f:", sum);
}
#include <stdio.h> void value(void); void main() { value(); } void value(void) { int yr = 1, period = 5, amnt = 5000, inrate = 0.12; float sum; sum = amount; while (yr <= period) { sum = sum * (1 + inrate); year = year + 1; } printf(" The total amnt is %f:", sum); }
#include <stdio.h>
void value(void);
void main()
{
    value();
}
void value(void)
{
    int yr = 1, period = 5, amnt = 5000, inrate = 0.12;
    float sum;
    sum = amount;
    while (yr <= period) {
        sum = sum * (1 + inrate);
        year = year + 1;
    }
    printf(" The total amnt is %f:", sum);
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The total amnt is 5000.000000
The total amnt is 5000.000000
The total amnt is 5000.000000

 

2. Function without arguments, with return value 

 An example is getchar function it has no parameters but it returns an integer and int type data that represents a character.

 

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Function declaration : int function();
Function call : function();
Function definition :
int function()
{
statements;
return x;
}
Function declaration : int function(); Function call : function(); Function definition : int function() { statements; return x; }
Function declaration : int function();
Function call : function();
Function definition :
                 int function()
                 {
                     statements;
                      return x;
                  }
    

Program for function without arguments, with return value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName();
}
void printName()
{
printf("StudyExperts");
}
#include<stdio.h> void printName(); void main () { printf("Hello "); printName(); } void printName() { printf("StudyExperts"); }
#include<stdio.h>  
void printName();  
void main ()  
{  
    printf("Hello ");  
    printName();  
}  
void printName()  
{  
    printf("StudyExperts");  
}  

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello StudyExperts
Hello StudyExperts
Hello StudyExperts

3. Function with arguments, without return value

When a function has arguments, it receives any data from the calling function but it does not return values.

 

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Function declaration : void function (int);
Function call : function( x );
Function definition:
void function(int x)
{
statements;
}
Function declaration : void function (int); Function call : function( x ); Function definition: void function(int x) { statements; }
Function declaration : void function (int);
Function call : function( x );
Function definition:
             void function(int x)
             {
               statements;
             }

Program for function with arguments, without return value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nsum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
#include<stdio.h> void sum(int, int); void main() { int a,b,result; printf("\nsum of two numbers:"); printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); sum(a,b); } void sum(int a, int b) { printf("\nThe sum is %d",a+b); }
#include<stdio.h>  
void sum(int, int);  
void main()  
{  
    int a,b,result;   
    printf("\nsum of two numbers:");  
    printf("\nEnter two numbers:");  
    scanf("%d %d",&a,&b);  
    sum(a,b);  
}  
void sum(int a, int b)  
{  
    printf("\nThe sum is %d",a+b);      
}  

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sum of two numbers:
Enter two numbers 10
20
The sum is 30
sum of two numbers: Enter two numbers 10 20 The sum is 30
sum of two numbers:

Enter two numbers 10 
20

The sum is 30

4. Function with arguments, with return value

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Function declaration : int function (int);
Function call : function( x );
Function definition:
int function(int x)
{
statements;
return x;
}
Function declaration : int function (int); Function call : function( x ); Function definition: int function(int x) { statements; return x; }
Function declaration : int function (int);
Function call : function( x );
Function definition:
             int function(int x)
             {
               statements;
               return x;
             }

Program for function with arguments, with return value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\sum of two numbers");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}
#include<stdio.h> int sum(int, int); void main() { int a,b,result; printf("\sum of two numbers"); printf("\nEnter two numbers:"); scanf("%d %d",&a,&b); result = sum(a,b); printf("\nThe sum is : %d",result); } int sum(int a, int b) { return a+b; }
#include<stdio.h>  
int sum(int, int);  
void main()  
{  
    int a,b,result;   
    printf("\sum of two numbers");  
    printf("\nEnter two numbers:");  
    scanf("%d %d",&a,&b);  
    result = sum(a,b);  
    printf("\nThe sum is : %d",result);  
}  
int sum(int a, int b)  
{  
    return a+b;  
}  

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sum of two numbers:
Enter two numbers:10
20
The sum is : 30
sum of two numbers: Enter two numbers:10 20 The sum is : 30
sum of two numbers:
Enter two numbers:10
20 
The sum is : 30   

 

Also read, Types of Function

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *