User defined function in C programming language
- Home
- function in C
- User defined function in C programming language
- On
- By
- 0 Comment
- Categories: function in C, function/method
User defined function in C programming language
User defined function in C programming language
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
- Function declaration
- function call
- Function definition
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
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
- function name – name of the function is sumNum()
- return type – the return type of the function is int
- arguments – two arguments of type int are passed to the function
Calling a function
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
Function Definition
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 }
The passing argument to a 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
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
Types of the user-defined function in C language
User defined function in C can be categorized into four types:
- function with no return value and without argument
- function with no return value and with an argument
- function with a return value and without argument
- function with a return value and with an argument
Function with no return value and without argument
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
Function with no return value and with the argument
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
Function with return value and without argument
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
Function with the return and with arguments
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