UrbanPro
true

Learn C Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Functions In C Programming

M Chandra Rao
18/05/2017 0 0

A C-language program is nothing but collection of Function, these are the building blocks of a ‘C’ program. Generally, a function mans a task.

                        “Function is a collection of logically related instructions which performs a particular task.”

                        Functions are also used for modular programming in which a big task is divided into small modules and all the modules are inter connected.

How to create the function (Function declaration):-

 

(parameter list)

{

Body of the function;

[return];

}

: It is specify the data type of the returning value.

:It is specify the name of the function.

(Parameter list): It is the list of parameters used in the function.

 

Function definition:-

                                    The collection of program statements in C that describes the specific task done by the function is called a Function definition. It consists of the function header and a function body.

            (parameter list)

 

            {

            ;

            ;

            ;

            }

Function header:

            Function header is similar to the function declaration but does not require the semicolon at the end. List of variables in the parenthesis are called Formal parameters. It consists of three parts:

  1. Return type.
  2. Function name.
  3. Formal parameters.

Example:

int add(a, b)

 

 

Function body:

            After the function header the statements in the function body (including variables and statements) called body of the function.

Example:

            int add (int x, int y)

            {

            return (x+y);

            }

Return statement:

                        Return statement is used return some value given by the executable code within the function body to the point from where the call was made.

General form:

                                return expression;

                                           or

                                 return value;

Where expression must evaluate to a value of the type specified in the function header for the return value.

            EX: return a+b;

                             Or

                        return a;

            If the type of return value has been specified as void, there must be no expression appearing in the return statement it must be written simply as

                                     return;

Function call (Function calling):

                                                      All functions within standard library or user-written, are called with in this main() function, the function call statements involves the function, which means the program control passes to that of the function. Once the function complete its task, the program control is passed back to the calling environment.

Syntax:

            <(parameter list)>;

                                      or

            variable name=<(parameter list)>;

Example:

1.Write a program to find the mean of the two integer numbers.

            #include

            int mean(int, int);

            main()

            {

            int p, q, m;

            clrscr();

            printf(“enter p,q values”);

            scanf(“%d%d”,&p,&q);

 

            m=mean(p,q);

            printf(“mean value is %d”,m);

            getch();

            }

 

            int mean(int x, int y)

            {

            int temp;

            temp=(x+y)/2;

            return temp;

            }

  1. Write a program that uses a function to check whether a given year is leap or not.

            #include

            void leap(int);

            main()

            {

            int year;

            clrscr();

            printf(“\n enter year \n”);

            scanf(“%d”,&year);

            leap(year);

            }

            void leap(int yr)

            {

            if(year%4==0&&year%100!=0||year%400==0)

            printf(“leap year”);

            else

            printf(“not leap year”);

            }

Types of functions:  

                                    The functions are also used for modular programming in which a big task is divided into small modules and all the  modules are interconnected.

Functions are basically classified as:

  1. Standard library functions(built-in functions).
  2. user defined functions.

Standard library functions:-

                                                library functions come along with the compiler and they can be used in any program directly. User can’t be modified the library functions.

Example:

sqrt();, printf();, clrscr();, abs();, strlen();, getch();etc………………

Example program:

            #include

            main()

            {

            int x;

            clrscr();

            printf(“enter x value \n”);

            scanf(“%d”,&x);

            printf(“square root value of x is %d”, sqrt(x));

            getch();

            }

user defined functions:     user defined functions is one which will be defined by the user in program to group certain statements together. The scope of a user defined function is to the extent of the program only, in which it has been defined. main is also a user defined function.

Syntax:

            )

            {

            Function body;

            ;

            }

Example program:

            #include

            main()

            {

            int r,rd;

            clrscr();

            printf(“enter radius for circle\n”);

            scanf(“%d”,&r);

            rd=area(r);

            printf(“area of the circle is %d”,rd);

            getch();

            }

            area(int x)

            {

            return(3.14*x*x);

            }

Types of parameters

            The nature of data communication between the calling function and the called function with arguments (parameter).

The parameters are two types:

  1. Actual parameters or original parameters.
  2. formal parameters or duplicate parameters.

 

main()

{

………….

Function1(a1, a2, a3,………am);

………….

}

 

Function1(x1, x2, x3,………xn);

{

  ……….

  …….....

  ……….

}

 

 

Actual parameters(arguments):

                        Actual parameters means original parameters for the function call. These parameters are declared at the time of function declaration. In the above example a1,a2,a3,…… am are the actual parameters. When a function call is made, only a copy of the values of actual arguments is passed into the called function.

Formal parameters (duplicate arguments):

                        Formal parameters means duplicate parameters for function call. These parameters  are declared  at the time of function definition. In the above example x1, x2, x3,………xn are the formal parameters, the actual and formal arguments should match in the number, type and order, the values of actual arguments are assigned to the formal arguments on a one to one basis, starting with the first argument.

 

Types of functions based on parameters:

            A function depending on whether arguments  are pres

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You



C Program-Temperature [conversion from farenheit to degree celsius]
//WAP to convert temperture from farenheit into degree celsius //Header files#include<stdio.h>#include<conio.h> //Main functrion void main(){ //Variable declaration of type float float...

Find out the Output of the following with reason and get C Language Training fess less by 10%
1. void main() { clrscr(); printf(5+"Beautifull"); getch(); } 2. void main() { int a=50; clrscr(); ...

C Program to print Block Letter and Small Case Alphabets using C
/* WAP to print Block Letter and Small Case Alpahbets using C*/ //Hint:use ascii code(value) to print #include#include void main(){ int i; clrscr(); //Block Letters Alphabets printf("Block Letters Alphabets\n");...
X

Looking for C Language Classes?

The best tutors for C Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Language with the Best Tutors

The best Tutors for C Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more