User Defined Function in C language

“Work hard in silence , and let the success be the noise”

Hello everyone!!!
I’m back with new article on user defined function.
Soo let’s start quickly 🙂

Introduction:-
The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function.

Syntax of Function:-
return_type function_name (argument list)
{
Set of statements – Block of code
}


Points to be noted while writing function:-
1) return_type: Return type can be of any data type such as int, double, char, void, short etc.
2) function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it’s name.
3) argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.
4) Block of code: Set of C statements, which will be executed whenever a call will be made to the function.

•Function Declarations/Function Prototype:-
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration has the following parts-
return_type function_name( parameter list );

•Suppose we need to write the prototype for function for adding two numbers then the prototype will be as follows:-
int sum (int num1, int num2);

Calling a Function:-
1)While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.
2) When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
3) To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

Now let us see some examples, which will make my concept more clear.

Q1) write a C program to add two numbers using functions.

#include<stdio.h>
int addition(int, int);
int addition(int num1, int num2)
{
int sum;
sum = num1+num2;
return sum;
}
main( )
{
int a,b,c;
printf(“Enter number 1: “);
scanf(“%d”,&a);
printf(“Enter number 2: “);
scanf(“%d”,&b);
c = addition(a, b);
printf (“Output: %d”, c);
}

Output:-
Enter number 1: 24
Enter number 2: 67
Output: 91

Q2) C Program using function to calculate maximum of two numbers.

#include<stdio.h>
int max(int num1, int num2);
int main ( )
{
int a = 100;
int b = 200;
int res;
res = max(a, b);
printf( “Max value is : %d\n”, res );
}
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

Output:-
Max value is : 200

This is the simple programs to make you’ll understand easily.
Will see some more related programs in the upcoming article. Soo keep reading 🙂

Hope you all must have got an idea about user defined function.
Soo see you all in the next article.. Till then good bye.
Have a wonderful day ahead 🙂






You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading