chapter 7 computer science class 10




        Chapter 7

        FUNCTION IN C



      EXERCISE

      1. What is a global variable in C? why do we need such a variable?

      The variable that are declared outside the function or block of the code is known as global variable. When make variable global any function in the program can directly access the variable.

       

      2. Write the syntax of a function declared. The name of function is calculateAge() the function accepts the current year and birth year of a person. The function returns the age of the person.

      int calculateAge(int currentYear, int birthyear);

       

      3. Write code segment for the function definition of the above function caluclateAge().

      int calculatege(int currentYear, int birthyear)

      {

      int age;

      age = currentYear – birthyear;

      return age;

      }

       

      4What are the different types of function? Differentiate among them.

      There are two types of function in C programing. They are-

      i) Library function : These are the function that are defined in C header files. For example to display output we use printf() function and to accept input we use sanf() function.

      ii) User defined function: These functions are created by programmer. These are called user defined function because the programmer writes the definition of these programs.

       

      5. Differentiate between caller and callee functions. Write a small C program to identify caller and callee function.

      A caller function is a function that calls another function. While callee is a function that was called. For example.

      #include<stdio.h
      int sum (int a, int b);

      Int main()                  // caller function

      {

      Int a=5, b=6 result;

      result =sum(a,b)    // callee function

      printf(“result %d”, result);

      return 0;

      }

      Int sum (int a, int b
      {

      Int result = a+b;

      return result;

      }

       In the above program , main() is a function and it is calling unction sum(). Thus main() can be called as caller function and sum()  Called as callee function.

       

      6.Why do we called a function user defined? Is printf() a user defined function justify.

      The function that is created by the programmer is called user defined function. These are called user defined function because the programmer write the definition of these definition of these programs. printf() is not a user defined function because its definition is already written in stdio.h header file. It is a library function.

       

      7. Can we have two functions with same name but with different number of parameters in a single C program? Write a simple program to justify it.

      No, we cannot have two functions with same name but with different number of parameters in a single program. For example:

      #include<stdio.h>

      int sum(int a, int b, int c);

      int sum (int a, int b);

      int main()

      {

          int a=5, b=6, c=7,s,r;

          s = sum (a,b,c);

          printf("sum%d", s);

          r= sum(a,b);

          printf("sum=%d",r);

          return 0;

      }

      #include<stdio.h>

      int sum(int a, int b, int c);

      {

          int sum = a+b+c;

          return sum;

      }

      int sum(int a, int b)

      {

          intsum = a+b;

          return sum;

      }

       

      The above program will return us a conflict function name error.

      8. what are different component of a function? Show with a complete C program?

      The different component of a component of a C program are as follows:

      Function declaration: A function must be declared before it uses. This is to tell the compiler about function name, function perimeter and return type.

      Function call: A function start execute when we make a call to it. A function can called anywhere in the program.

      Function definition: This portion of the function contain actual statements that are executed we the function is called.

      A simple C Program to demonstrate function declaration, call and definition.

      #include <stdio.h>

      int sum(int a, int b);    // Function declaration

      int main()

      {

          int a = 5, b=6, r;

          r = sum(a,b);               // Function call

          printf("sum=%d",r);

          return 0;

      }

      int sum(int a, int b)        // Function definition

      {

          int sum = a+b;

          return sum;

      }

       

      9. Define recursive function? Can we user recursive function to solve all kind of problems.

      If a function call itself we call it recursion, and the function is called as recursive function. The                    syntax of recursive function is given below. Here we are calling f() function from the definition of f().

      void f()

      {

      ----------------------

      f();

      ----------------------

      }

      In C programming recursion is useful for performing mathematical calculating, sorting, searching etc. however it is not applicable to solve all kind of problems.

       

      10. Consider the code and list all the syntax errors.

       

      # include<stdio.h>

      int fun(int x)

      {

         if (x%2==0)

       return 1;

      else

      return 0;

      }

      int main()

      {

       int number;

      printf(“Enter the number:”);

      scanf(“%d”, &number);

      int x = fun();

      return 0;

      }

       

       

      There was no parameter pass through function fun() while it was called. The correct syntax is

      int x = fun(number);

      The output of the function fun() is not displayed after calling it.

      The correct syntax is

      int main()

      {

       int number;

       printf(“\nEnter the number:”);

      scanf(“%d”, &number);

      int x = fun(number);

      printf(“Result: %d”, x);

      return 0;

      }

       

      11. Consider the segment below and find out the output if the user enter 5 from the keyboard when asked for.

      # include<stdio.h>

      int fun(int x)

      {

         if (x%2==0)

       return 1;

      else

      return 0;

      }

      int main()

      {

       int number;

      printf(“\nEnter the number:”);

      scanf(“%d”, &number);

      int x = fun(number);

      printf(“%d”,x);

      return 0;

      }

       

       The program will display the output as 0 when the user enter 5 from the keyboard. Because when the user enter 5 from the keyboard it will pass a function fun(). Inside function fun() it will divide 5 by 2 and check if remainder I 0 or not. As the remainder is 0 so the function fun() will return 0 and it will store to the variable n and finally it will display the output as 0.

       

      12. Write C program and define a function square() that accepts a number as the parameter and return square of the number as output.

      Answer

      #include <stdio.h>

      int square(int a);

      int main()

      {

          int number;

          printf("Enter the Number\n");

          scanf("%d",&number);

          int sq = square(number);

          printf("Square of %d is %d", number, sq);

          return 0;

      }

      int square(int a)

      {

          int sq = a*a;

          return sq;

      }

       

      13. Write a C program and define a function search () that searches an element in an array and return the index of the element.

      Answer

      #include <stdio.h>

      int search(int,int);

      int num[5];

      int main()

      {

          int size, i, n, index;

          printf("Enter the number of Elements: \n");

          scanf("%d",&size);

          for(i=0;i<size;i++)

          {

              printf("Entered Element %d:\n",i+1);

              scanf("%d", &num[i]);

          }

          printf("Enter the element to be searched\n");

          scanf("%d",&n);

          index = search(size,n);

          printf("%d found at index %d\n",n,index);

          return 0;

      }

      int search(int size, int n)

      {

          int i, index;

          for (i=0;i<=size;i++)

          {

              if(num[i]==n)

              {

                  index =i;

                  break;

              }

          }

          return index;

      }

       

      14. Write a C program and define a recursive function to find the summation of the first N natural numbers.

      Answer:

      #include <stdio.h>

      int addNum(int);

      int main()

      {

          int n,sum;

          printf("Enter a Positive integer\n");

          scanf("%d", &n);

          sum = addNum(n);

          printf("Sum = %d", sum);

          return 0;

      }

      int addNum(int n)

      {

          if(n!=0)

              return n+addNum(n-1);

          else

              return n;

      }

       

      15. Write a C program and define a function add() that accepts three integers. These integers indicate indices of an integer array. The function returns the summation of the elements stored in those indices.

      7

      8

      8

      0

      0

      9

      For  example, if we call the function add (0,2,5), the function will return 24. The output is formed by7 +8+9 because elements at indices 0,2 and 5 are 7, 8 and 9 respectively.

      Answer:

      #include <stdio.h>

      int num[50];

      int add(int, int, int);

      int main()

      {

          int i,n,sum;

          printf("Enter nos of Elements: \n");

          scanf("%d", &n);

          for(i=0;i<n;i++)

          {

          printf("Enter value of indices %d\n",i);

          scanf("%d",&num[i]);

          }

          sum = add(0,2,5);

          printf("Sum = %d",sum);

          return 0;

      }

      int add(int a, int b, int c)

      {

          int sum = num[a]+num[b]+num[c];

          return sum;

      }

       Extra Question

       

      1. What is function in computer programming?

      A function is a block of statements that is used to perform a single task.

       

      2. What is function name?

      The valid name of the function is called function name

       

      3. What is return type?

      The type of data that the function is returning is called return type.

       

      4. What is function parameter?

      We pass or send some data to a function when it executes. It is called function parameter.

       

      5. Name the three pats of functions in C.

      Three parts of Function are:

      i. Function declaration

      ii. Function call

      iii. Function definition

       

      6. What is function declaration? Give example.

      Function declaration means to tell the compiler about the function name, function parameters and return type for example: int findSum( int a, int b);

       

      7. What is function call? Give example.

      Function call is responsible for the execution of the function. A function starts execution when it calls. For example: int result = findSum(a,b);

       

      8. What is definition?

      The portion of the function that contains the actual statements that are executed when function is called.

       

      9. How many types of Functions in C programing? Name them.

      There are two types of functions: Library function and User defined function

       

      10.What is library function?

      The function that are defined in C header file is called libr function.

       

      11. What is user define function?

      The program created by the programmer is called user defined function.

       

      12. What is recursive function?

      The function that call itself is called recursive function.

       

      13. What Type of program uses recursive function.

      The function which uses mathematical pattern uses recursive function.

       

      14. What will happen if we keep different parameter in function definition and function call?

      We will get a conflict type error.

       

      15. What is Global variable?

      The variable declared outside of scope or function is called global variable.

       

      16. What is local Variable?

      The function declared inside a function is called local variable.

       

      17. When a function become void?

      If the function does not return anything its return type becomes void.

       

      18. Can function return more than one item?

      No, a function can return only one item.

       

      19. What is another name of parameter?

      Argument

       

      20. When does a function start execution?

      A function start execution when we call to it.

       

      25. Write the syntax of recursive function?

      function1()

      {

      --------------

      Function1();

      ------------------

      }


      26. Can we write a C program without writing the main function?

      No, we can not write a C program without using a main function. It is the point from where compiler start compiling.

       

      27. Write a C program to find the simple interest by using function.

      #include <stdio.h>

      float SimpleInt(float p, float r, float t)

      {

          float si;

          si = (p*r*t)/100;

          return si;

      }

      int main()

      {

        float principle, time, rate, SI;

        printf("Enter Principle (Amount)\n");

        scanf("%f",&principle);

       

        printf("Enter Time\n");

        scanf("%f",&time);

       

        printf("Enter Rate\n");

        scanf("%f",&rate);

       

        SI = SimpleInt(principle, rate, time);

        printf("Simple Interest = %f",SI);

        return 0;

      }

       

      28. What is function? Mention the advantages of using function in programming. How function can be categorized in C?

      A function is a block of statements that is used to perform a single task.

      The advantages of function are

      i. we can reuse the part of a program.

      ii. We can divide program into separate functions.

      iii. Function makes a long program short and easy to understand.

      Function can be categorized in two parts: Library function and User defined function.




    Table of Content