Functions in C Programming

What is a Function?

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind

The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.

We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

Structure of a Function

There are two main parts of the function. The function header and the function body.

Function Header

In the first line of the above code

It has three main parts

  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in parenthesis
  3. Return value type i.e. int

Function Body

What ever is written with in { } in the above example is the body of the function.

Function Prototypes

The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like

The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.

105 Comments

    • guys, can you help me out with this prob: Make a program that would ask for two numbers then display their quotient aand modulo without using operators / and %.

    • lets say a is dividend and b is divisor, then do a-b until a<b, keep count, and count is the quotient. and value of a ( which is now < b) is remainder.

    • function is used for reusability of the code..the code inside the program can used anywhere in the program by calling the name of the function
      and also it used for easy understanding of the code functionality…

  1. write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..
    SAMPLE INPUT/OUTPUT DIALOGUE:
    enter a list of numbers:
    5 4 8 2 6

    enter a number to be searhed: 2
    2 is found in location 4

    • hi faizan., i cant i understand ur program., u have called a function but where is function definition…?

    • n!/n^n (from n=1 to infinity)
      Write a C program which will find the sum of the N-terms of the aboveseries. The program will consist of a MAIN function and a function.
      Within the MAIN function.
      . Number of terms N will be read from the standard input.
      . N will be passed to the function as an argument.
      . Sum of the N terms(return value from the function) will be printed.
      Within the function.
      . Sum of the N terms of the given series will be calculated and returned to the MAIN function.

  2. Detail About Recursion and its Type

    Here I am going to give a detail about Recursion in C++.
    Definition: Recursion is the process where a function is called itself but stack frame will be out of limit because function call will be infinite times. So a termination condition is mandatory to a recursion.
    In C++, Recursion can be divided into two types:
    (a) Run- Time Recursion: Normal as in C
    (b) Compile- Time Recursion: By using Template

    Each of these can be also divided into following types:

    1. Linear Recursion
    2. Binary Recursion
    3. Tail Recursion
    4. Mutual Recursion
    5. Nested Recursion

    1. Linear Recursion: This recursion is the most commonly used. In this recursion a function call itself in a simple manner and by termination condition it terminates. This process called ‘Winding’ and when it returns to caller that is called ‘Un-Winding’. Termination condition also known as Base condition.

    Example: Factorial calculation by linear recursion

    Run-Time Version

    Winding Process:

    Function called Function return

    Fact(6) 6*Fact(5)
    Fact(5) 5*Fact(4)
    Fact(4) 4*Fact(3)
    Fact(3) 3* Fact(2)
    Fact(2) 2* Fact(1)
    Fact(1) 1* Fact(0)
    Terminating Point
    Fact(0) 1

    Unwinding Process

    Fact(1) 1*1
    Fact(2) 2*1
    Fact(3) 3*2*1
    Fact(4) 4*3*2*1
    Fact(5) 5*4*3*2*1
    Fact(6) 6*5*4*3*2*1

    Compile-Time Version

    To test it how it’s working at compile time, just call
    cout << Fact<-1>::factVal ;
    And compile it then compiler error will come, because no template for -1.

    2. Binary Recursion: Binary Recursion is a process where function is called twice at a time inplace of once at a time. Mostly it’s using in data structure like operations for tree as traversal, finding height, merging, etc.

    Example: Fibonacci number

    Run Time Version Code:

    Compile Time Version Code

    3. Tail Recursion: In this method, recursive function is called at the last. So it’s more efficient than linear recursion method. Means you can say termination point will come(100%) only you have to put that condition.

    Example: Fibonacci number

    Run Time Version Code:

    Compile Time Version Code

    template <int n, int x, int y>
    
    struct FibNum
    {
       // Recursive call By tail method
       enum 
      { 
            val = FibNum<n-1, y, (x+y)>::val
       };
    };
    
    // Base Condition or Termination 
    template<int x, int y>
    struct FibNum<1, x, y>
    {
       enum 
    
    • Please help me to know how to make a program in C language in finding the factorial of N using for, while and else in the body statements?
      thanks….
      ——– Original Message ——–
      Detail About Recursion and its Type

  3. I would like to know how to write source code for this assignment:
    Write a new “Currency Conversion” program, with a title, to accept one input currency, which is error checked as a valid entry, and then display its equivalency in US dollars. For example: 300 Canadian Dollar = 189.19 US Dollar.

  4. .phelp nmn po. my project kme. ang problema ayaw bumlik nung mga value, anong gagawin ko?? my alam po ba kau program. ung pang hyperlink, halimbawa, pag a ung pinundot, pupunta sa new page, mala gnun po.. salamat po sa tutulong, badly needed.. salamat.

    • no
      every c function doesn’t return 0
      but a function can return value is integer
      generally 1(when function is executed successfully),-1(Abnormal termination) and 0 (when nothing is retuned)

      if we return a integer 76 or 68 it does not matter

      actually
      why a function should return a value?
      ans- if a function does not return value then how can processor knew that that functionality is finished and other function is to executed so a function should return a value which is by default integer if not mentioned as void

    • When we use int main() in the place of main we must write return0; at the end of the program. Not for all the programs

  5. what is type of main() function in c?
    is it predefined or userdefined?
    if it is predefined then where is it stored in the files of c?

  6. plz help me how can i make this program
    calculate the average of num by using function that reading the num to be averaged, calculating their sum as they are reading the second funcion should complete the deviation of each num about the average

  7. write program in C to do the basic arithmethic operation (addition, subtraction, multiplication and division). Each operation must have it’s own function. The main function must call all the functionwith the same data set.
    and write its psedu code?

  8. I need help with this. I have an idea of what to do just don’t know how to start implementing it and my text book is absolutely useless.

    Write a function called “parkingCharge” that, given the type of vehicle (c for car, b for bus, t for truck) and the hours a vehicle spent in the parking lot, returns the parking charge based on these rates: car = $2 per hour, bus = $3 per hour, truck = $4 per hour.

  9. plz help me how can i can solve this program.

    suppose x,y,z are floating-point variables that have been assigned the values x=8.8, y=3.5, z=5.2 . determine the value of each of the following arithmetic expressions:

    a) x%y

  10. hello all. I am still new on this programme (C source code). Need your help on this.

    int i=8, j=5;
    float x=0.005, y=-0.01;
    char c=’c’, d=’d’;

    determine the value of each of the following expression. use the values initially assigned to the variables for each expressions.

    (a) (3*i-2*j)% (2*d-c)

    (b) 2*((i/5)+(4*(j-3))%(i+j-2))

  11. please help on this program am really confusion to solve

    From the given input array remove the negative elements and then sort the remaining array
    elements, store that into the output array. Consider input1 is array elements and input2 is array
    size.

  12. hello can any1 help me to solve this 1?

    1)Write a simple exercise to calculate the sum of 5 numbers. You are required to enter the numbers in the main function, however, the calculation is done in the Calculate() function. Another function that is called display() function will display all the numbers that you have entered. It will also display the sum and the average values.

    and this 1
    2)Use a single array to solve this problem. A company pays its sales-people on the commission basis. The salespeople receive RM200 per week plus 9 percent of their gross sales for that week. For example, a saleperson who grosses RM3000 in sales in a week receives RM200 plus 9 percent of RM3000, or a total of RM470. Write a program (using an array) that determines how many of the salespeople earned salaries in each of the following ranges:
    a) RM 200 – RM 300
    b) RM 301 – RM 500
    c) Above RM 500
     Hints :
    o Find the input, process and output first
    o Write the algorithm
    o Evaluate the algorithm whether it corrects or not
    o Work in a group to discuss how to solve the problem

    thnkz

Leave a Reply