Basic computer course, C programming language, Education, Engineering Course, Engineering Courses, Maths Coaching, online computer coaching, PGDCA course coaching, programming course, school computer coaching, training, Uncategorized, Web Design course

User Defined Function in C-Tccicomputercoaching.com

What is User Defined Function?

A User Defined Function is a block of code developed by user to perform specific task.

Using user Defined Function we can divide a complex problem into smaller modules. So we can decrease complexity and lengthy.

C allows programmer to define functions according to their need. These functions are known as user-defined functions. For example:

Suppose, a program which includes addition, subtraction, division and multiplication of two numbers in same program. You can create user defined functions for each operation individually to solve this problem:

c programming

For example,

#include <stdio.h>

#include <conio.h>

Void add(int, int); // function prototype

Void sub (int, int); // function prototype

Void mul (int, int); // function prototype

Void div (int, int); // function prototype

void main ()

{

inta,b;

printf(“enter the value of a and b);

scanf(“%d %d”,&a,&b);

clrscr ();

add (a,b);

sub (a,b);

mul (a,b);

div (a,b);

}

Void add (int x,int y)

{

Int c;

C=x+y;

printf(“%d”,c)

}

void sub ()

{

Int c;

C=x-y;

printf (“sub=%d”,c)

}

Void mul ()

{

int c;

C=x *y;

printf (“%d”, c)

}

Void div ()

{

int c;

C=x/y;

Printf(“div=%d”,c)

}

User Defined Function has 3 sections:

Function Prototype

Function Call

Function Definition

Function prototype:

Syntax of function prototype :

returnTypefunctionName(type1 argument1, type2 argument2,…);

A function prototype is simply the declaration of a function that specifies function’s name, parameters and return type. It doesn’t contain function  body. Parameters names are optional.

Function Call:

Syntax of Function Call:

Function name (argument1, argument2);

We can use only value also. i. e. add (5, 6);

Compiler starts compilation from main () function, so to jump control from main to user defined function, have to use function call. When this function call executes, control jumps from main to that particular user defined function.

Function Definition:

Syntax of Function Definition:

returnTypefunctionName(type1 argument1, type2 argument2,…)

{

Body of function

}

When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes insid the body of a function.

Passing arguments to a function:

In programming, argument refers to the variable passed to the function. In the above example, two variables a and b are passed during function call.

The parameters x and y accepts the passed arguments in the function definition. These arguments are called formal parameters of the function.

The type of arguments passed to a function and the formal parameters must match, otherwise the compiler throws error.

Return Statement:

The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after return statement.


TCCI-Tririd Computer Coaching 
Institute is focused on providing Quality education with practical sessions. At TCCI student can learn various programming languages like cc++, Java, Data Structure, Python etc…

If you like this post then please share and like this post.

Call us @ 98256 18292.

Visit us @ tccicomputercoaching.com

 

Leave a comment