0% found this document useful (0 votes)
2 views

C Function Examples

The document provides examples of different types of functions in C programming, including functions with no arguments and no return values, functions with no arguments but with return values, functions with arguments and no return values, and functions with arguments and return values. It includes code snippets demonstrating how to declare, define, and call these functions. The examples illustrate basic operations such as addition and user input handling.

Uploaded by

euphonia07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Function Examples

The document provides examples of different types of functions in C programming, including functions with no arguments and no return values, functions with no arguments but with return values, functions with arguments and no return values, and functions with arguments and return values. It includes code snippets demonstrating how to declare, define, and call these functions. The examples illustrate basic operations such as addition and user input handling.

Uploaded by

euphonia07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Function with no arguments and no return value Function with no arguments and with return value

#include <stdio.h> // header file #include <stdio.h> // header file

void addnumber(); // function declaration // function declaration


int getnumber_1();
int main(){ int getnumber_2();
addnumber(); // argument is not passed
return 0; int main(){
} int num1, num2;
num1 = getnumber_1(); // no argument is passed
// return type is void meaning does not return any value num2 = getnumber_2(); // no argument is passed

void addnumber() { // function definition printf("\n%d plus %d is %d", num1,num2,num1+num2);


int func_num1=4, func_num2=6; return 0;
int total = func_num1+func_num2; }
printf("%d plus %d is %d", func_num1, func_num2, total);
} // returns integer entered by the user

int getnumber_1(){ // function definition


int func_num1;
Function with arguments and with no return value printf("\nEnter any integer: ");
scanf("%d", &func_num1);
#include <stdio.h> // header file return func_num1;
}
// function declaration
void addnumber(int func_num1, int func_num2); int getnumber_2(){ // function definition
int func_num2;
int main(){ printf("\nEnter any integer: ");
int num1, num2; scanf("%d", &func_num2);
printf("Enter two integers: "); return func_num2;
scanf("%d %d",&num1,&num2); // pass arguments }
addnumber(num1,num2); // function call
return 0;
}
Function with arguments and with return value
// return type is void meaning does not return any value
#include <stdio.h> // header file
// function definition
void addnumber(int func_num1, int func_num2){ // function declaration
int total = func_num1+func_num2; int addnumbers(int func_num1, int func_num2);
printf("%d plus %d is %d",func_num1, func_num2, total);
} int main(){
int num1, num2, sum;
printf("Enters two numbers: ");
scanf("%d %d", &num1, &num2); // pass arguments
sum = addnumbers(num1, num2); // function call
printf("Sum = %d", sum);
return 0;
}

// return value type is int (integer)

// function definition
int addnumbers(int func_num1, int func_num2){
int total;
total = func_num1+func_num2;
return total;
}

You might also like