We will learn about User defined function in the C programming language.
C programming language allows coders to define functions to perform special tasks. As functions are defined by users, they are called user-defined functions.
user-defined functions have contained the block of statements which are written by the user to perform a task
Elements of user-defined function in C
the function has three related elements, in order to establish the function
Here is an example to find the sum of two numbers. a user-defined function sumNum() is created to perform this task
#include <stdio.h> #include <stdlib.h> int sumNum(int x,int y); //function declaration int main() { int i,j,sum; printf("Please enter 2 numbers for find sum\n"); scanf("%d %d",&i,&j); sum=sumNum(i,j); //function call printf("The result of sum is :%d",sum); getch(); return 0; } int sumNum(int x, int y)//function definition { int result; result=x+y; return result; //return statements }
When the above code is compiled and executed, it produces the following results
Please enter 2 numbers for find sum 34 45 the result of sum is :79
we will discuss user-defined function using the above example
declaration of the user-defined function.
A function declaration is a frame (prototype)of function that contains the function’s name, list of parameter and return type and ends with the semicolon. but it doesn’t,t contain the function body
Syntax
return_Type function_Name(parameter1,parameter2,......);
Example
Example of the function declaration
int sum_Num(int x, int y,.........);
In the above example, int sumNum(int x, int y); is a function declaration which contains the following information
Every function declaration must contain the following 3 parts and ends with the semicolon in C language
Syntax
function_Name(Argument list);
Example
sum_Num(argument_1, argument_2,.......);
When the function is called, the control flow of the program move to function definition and statements executes the inside body of the function
The function definition is an expansion of function declaration. It contains codes in the body part of the function for execution program by the compiler – it contains the block of code for the special task
The syntax of the function definition
return_Type function_name(parameter_1, parameter_2,....){ //statements //body of the function }
In the function of C programming language, when we called the function it used to passed value to the variable referred by argument. Above the program x,y are contained two parameters in the function and i,j are arguments of the function
In the above example, two variable i,j passed during the function call for passed arguments in the function definition.
When we passed an argument to the function, type of argument to the function and list of formal parameters must be matched. it is not, the compiler will throw an error
Return statements terminate the execution of the function and return value to calling the function. Also, return statements control of the program control and moved to the calling function.
Syntax
return (expression);
Example
return (result); return (x+y);
In every function, function definition should be followed by the prototype of function declaration, if it is not, the compiler throws an error of the program.
Program 1
#include <stdio.h> #include <stdlib.h> int division(int x,int y); //function declaration or prototype int main() { int i,j,div; printf("Please enter 2 numbers for division\n"); scanf("%d%d",&i,&j); div=division(i,j); //function call printf("The result of division is :%d",div); getch(); return 0; } int division(int i, int j) { int result; result=i/j; return result; //return statements }
When the above code is compiled and executed, it produces the following results
Please enter 2 numbers for division 45 9 The result of division is :5
User defined function in C can be categorized into four types:
Add two numbers using the function
Program 1
#include <stdio.h> #include <stdlib.h> void add(); int main() { add(); getch(); return 0; } void add() { int a=10; int b=12; int c=a+b; printf("Total value of(a+b) is :%d\n",c); }
When the above code is compiled and executed, it produces the following results
Total value of(a+b) is : 22
Program 2
using get input method from user
#include <stdio.h> #include <stdlib.h> void add(); int main() { add(); getch(); return 0; } void add() { int a,b; printf("Enter your first number for a :"); scanf("%d",&a); printf("Enter your first number for b :"); scanf("%d",&b); int c=a+b; printf("Total value of(a+b) is :%d\n",c); }
When the above code is compiled and executed, it produces the following results
Enter first number for a 12 Enter second number for b 23 total number (a+b) is 35
In the above program, add() function take input from the user find total value and displays result on the screen – No argument passed and no return value
Find the difference between the two numbers using the function
Program 1
#include <stdio.h> #include <stdlib.h> void main() { diff(25,15);//calling function with arguments getch(); return 0; } void diff(int a,int b)//argument is passed to this function {//void data type never return value from function int c=a-b; printf("different between numbers (a-b) is %d\n",c); }
When the above code is compiled and executed, it produces the following results
different between numbers (a-b) is 10
Program 2
using get input method
#include <stdio.h> #include <stdlib.h> int main() { diff();//called the function inside the main getch(); return 0; } void diff(int a, int b)//argument is passed to this function {//void never return value from function printf("Enter first number for a\n"); scanf("%d",&a); printf("Enter second number for b\n"); scanf("%d",&b); int c=a-b; printf("different between (a-b) is %d\n",c); }
When the above code is compiled and executed, it produces the following results
Enter first number for a 35 Enter second number for b 15 different between (a-b) is 20
In the above program, diff() function take input from the user find different value and displays result on the screen – argument passed but no return value
Program 1
#include <stdio.h> #include <stdlib.h> int main() { printf("answer is:%d",mul()); getch(); return 0; } int mul() { int f_no=12; int s_no=22; int multi; multi=f_no*s_no; return(multi); }
Program 2
using get input method
#include <stdio.h> #include <stdlib.h> int main() { printf("answer is:%d",mul()); getch(); return 0; } int mul() { int f_no; int s_no; printf("Enter first number :"); scanf("%d",&f_no); printf("Enter second number :"); scanf("%d",&s_no); int multi; multi=f_no*s_no; return(multi); }
When the above code is compiled and executed, it produces the following results
Enter first number for a :11 Enter second number for b :22 multiple total is :242
In the above program, mul() function take input from the user find multiple value and displays result on the screen – No argument passed and with the return value
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int result=divi(50,5); printf("Divided value: %d\n",result); getch(); return 0; } int divi(int a, int b) { int temp=a/b; return temp; }
When the above code is compiled and executed, it produces the following results
divided value :10
Program 2
using get input method
#include <stdio.h> #include <stdlib.h> int main() { int result=divi(); //called the function printf("answer is:%d",result); getch(); return 0; } int divi(int num1, int num2) //argument passed to the function { printf("Enter first number :"); scanf("%d",&num1); printf("Enter second number :"); scanf("%d",&num2); int temp=num2/num1; return temp; }
When the above code is compiled and executed, it produces the following results
Enter the first number :9 Enter the second number :45 answer is :5
In the above program, divi() function take input from the user find divided value and displays result on the screen – argument passed and with the return value
Similar Functions
Other C programming language functions that are similar to the cosine function
suggested for you
User defined function in C language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…