Functions

1. Break down the following function declarations to highlight return type, function name, and number of parameters along with their names and data types.

a. int largest_factor (int num);

Ans:

Function name: largest_factor

Return type: int

Number of parameters: 1

List of parameters:

Parameter num of type int

 

b. float celsius_to_fahrenheit(float celsius);

Ans:

Function name: celsius_to_fahrenheit

Return type: float

Number of parameters: 1

List of parameters:

Parameter celsius of type float

 

c. float average (int maths, int science, int english);

Function name: average

Return type: float

Number of parameters: 3

List of parameters:

Parameter maths of type int

Parameter science of type int

Parameter english of type int

 

 

2. Which of the following statements are true.

a. When a function is called, control jumps from the calling function to the beginning of called function.

Ans: True

 

b. A function can return any number of values.

Ans: False. A function can return only one value.

 

c. A function can have any number of parameters.

Ans: True

 

d. A function must have at least one parameter.

Ans: False. A function can have any number of parameters, including 0.

 

e. When a called function has been fully executed, control returns to calling function to the same point from where function was called.

Ans: True

 

f. return statement indicates function processing is complete and causes control to jump back to calling function.

Ans: True

 

 

3. Write a function to compute average of three numbers.

float average(int val1, int val2, int val3) {
    return (val1 + val2+ val3)/3.0;
}

 

 

4. Write a function to convert temperature from Celsius to Fahrenheit.

float celsius_to_fahreheit(float cel) {
    return ((cel *9)/5) + 32;
}

 

 

5. Write a function to find maximum of two integers. Use this function to determine maximum of three integers.

int max_2(int val1, int val2) {
    if(val1 > val2) {
        return val1;
    }
    return val2;
}

int max_3(int val1, int val2, int val3) {
    int higher = 0;
    higher = max_2(val1, val2);
    higher = max_2(val3, higher);
    return higher;
}

 

 

6. Identify all local variables and their scope for table printing example that we saw earlier in this chapter.

01: #include <stdio.h>

02: int print_table(int n);

03: int main (int argc, char * argv[]) {
04:     int num=0, i=0;

05:     printf("Enter a number >2: ");
06:     scanf("%d",&num);

07:     if (num<=2) {
08:         printf("Invalid input\n");
09:     } else {
10:         for(i=2; i<=num; i++) {
11:             print_table(i);
12:         }
13:     }

14:     return 0;
15: }

/* Print table of n */
16: int print_table(int n) {
17:     int i=0;

18:     for(i=1; i<=10; i++) {
19:         printf("%d X %d = %d\n", n,i,n*i);
20:     }
21:     return 1;
22: }


Ans:

Function main has local variables num and i. Their scope is limited to within function main, i.e. from line 04 when it comes into existence to line 14 when main returns.

Function print_table has local variable i. Its scope is limited to within print_table function, i.e. from line 17 when it comes into existence to line 21 when print_table returns.

 

7. Which of the following statements are true.

a. All functions of a program share the same symbol table and have common memory regions.

Ans: False. Each function call has its own designated stack area.

 

b. If we have declared a variable in a function then we cannot use the same variable name in another function.

Ans: False. Variables local to a function are not visible in another function.

 

c. Every function must have at least one parameter.

Ans: False.

 

d. 3plus5 is a valid function name.

Ans: False. Function names have to begin with an alphabet or _.

 

e. There is no limit on number of functions that can be defined in a program.

Ans: True

 

f. We can define any number of functions inside another function.

Ans: False. We can call any number of functions from within a function but we cannot define functions inside a function.

 

8. What will be the return value of triple_it function for following arguments.

01: #include <stdio.h>

02: int triple_it(int num);

03: int main (int argc, char * argv[]) {
04:     int input = 3, output = 0;
05:     output = triple_it(input);
06:     printf("Value of input after function call = %d\n",input);
07:     printf("Value of output after function call = %d\n",output);

08:     return 0;
09: }

10: int triple_it(int num) {
11:     num = num * 3;
12:     printf("Value of num in function = %d\n",num);
13:     return num;
14: }


float pi = 22.0/7;

Ans: Value of pi is 3.14285. When it is passed as argument on line 05, it gets typecasted to data type of parameter – int. We know that typcasting float to int leads to dropping entire fraction part of the number. Hence 3 gets passed as parameter. Consequently, return value of tripe_it is 9.

 

char pi = ‘p’;

Ans: We know that ASCII encoding of ‘p’ is 112. Value 112 is what gets passed to tripe_it. Consequently, return value of tripe_it is 336.

 

double pi = -4.98765;

Ans: When pi is passed as argument on line 05, it gets typecasted to data type of parameter – int. We know that typcasting double to int leads to dropping entire fraction part of the number. Hence -4 gets passed as parameter. Consequently, return value of tripe_it is -12.

 

9. What will be the output of following program.

#include <stdio.h>

float add5(int num);

int main (int argc, char * argv[]) {
    float pi = 3.14;
    float result = 0;

    result = add5(pi);
    printf("Result %f\n",result);
    return 0;
}

float add5(int num) {
    return num + 5.5;
}

Ans: 8.500000

 

10. Trace control flow of below program for finding nth prime number for user input 6.

#include <stdio.h>

int is_prime (int num);

int main (int argc, char * argv[]) {
    int num = 0, count = 0, i = 0;
    printf("Enter a number between 5 and 100: ");
    scanf("%d",&num);

    if (num < 5 || num > 100) {
        printf("Incorrect input\n");
    } else {
        i=3; count=1;
        while(1) {
            if (is_prime(i)) {
                count++;
                if (count == num) {
                    printf("%dth prime number is %d\n", num, i);
                    break;
                }
            }
            i++;
        }
    }
}

int is_prime(int num) {
    int i=0;
    for(i=2; i<=num/2; i++) {
        if (num%i== 0) {
            return 0;
        }
    }
    return 1;
}

 

 

11. What will be the output of following program

#include <stdio.h>

int func1 (int num1, int num2);
int func2 (int param);

int main (int argc, char * argv[]) {
    int a=3, b=7, c=10, d=0;
    d = func1(a, d);
    c = func2(b);
    b = func1(d, c) + func2(a);
    a = func2(a);
    printf("%d %d %d %d\n", a,b,c,d);
}

int func1 (int num1, int num2) {
    num1 = num1 + 3;
    num2 = num2 - num1;
    return ((num1 + num2) * 2);
}

int func2 (int param) {
    return (param * param);
}

 

Ans: 9 107 49 0

 

12. Write a function that determines whether a number is cube of a natural number.

int is_cube(int num) {
    int i = 1;
    do {
        if(i * i * i == num) {
            return 1;
        }
        i++;
    } while (i * i * i <= num);
    return 0;
}

 

 

13. Write a function that determines whether a number can be expressed as sum of two prime numbers.

#include <stdio.h>

int is_prime(int num) {
    int i=0;

    for(i=2; i<=num/2; i++) {
        if (num%i== 0) {
            return 0;
        }
    }

    return 1;
}

int is_sum_of_prime(int num) {
    int i=0;
    for(i=2; i<=num/2; i++) {
        if(is_prime(i)) {
            if(is_prime(num-i)) {
                return i;
            }
        }
    }

    return 0;
}

int main (int argc, char * argv[]) {
    int num = 0, prime=-1;

    printf("Enter a number > 2: ");
    scanf("%d", &num);

    if (num > 2) {
        prime = is_sum_of_prime(num);
        if (prime) {
            printf("%d + %d = %d\n", prime, num-prime, num);
            return 0;
        }
    } else {
        printf("Invalid input");
    }
    if(!prime) {
        printf("Not a sum of primes");
    }
    return 0;
}

 

 

14. Which of the following statements are true.

a. void is a data type.

Ans: True.

 

b. Local variables of called function are visible to calling function.

Ans: False. Visbility of local variables is limited to the function itself.

 

c. A function that has return type void cannot have any parameters.

Ans: False. It can have any number if parameters.

 

d. return; is a valid C statement.

Ans: True.

 

e. A function with non void parameters cannot have void return type.

Ans: False. void print_table(int num); is an example.

 

f. int func(int a, void b, float c);

is a valid function declaration.

Ans: False. A parameter cannot be of type void.

 

15. Modify the hello example that included hello_asia, hello_india and similar functions such that it should compile without any warnings.

#include <stdio.h>

void hello_india(void){
    printf("Hello from India\n");
}

void hello_china(void){
    printf("Hello from China\n");
}

void hello_asia(void){
    printf("Hello from Asia\n");
    hello_india();
    hello_china();
}

void hello_canada(void) {
    printf("Hello from Canada\n");
}

void hello_usa(void) {
    printf("Hello from USA\n");
}

void hello_north_america(void) {
    printf("Hello from North America\n");
    hello_canada();
    hello_usa();
}

int main(int argc, char * argv[]) {
    printf("Hello World from main!\n");
    hello_asia();
    printf("Hello again from main!!\n");
    hello_north_america();
    printf("Hello one more time from main!!\n");

    return 0;
}

 

16. What will be the output of following program.

#include<stdio.h>

int silly_function (int num1, int num2) {
    num1 += num2;
    num2 -= 3;
    return (num1+num2);
}

int main (int argc, char * argv[])  {
    int num1 = 5, num2 = 9;
    num2 = silly_function(num1, num2);
    printf("%d %d\n", num1,num2);
}

Ans: 5 20

 

 

17. Try out above calls to triple_it and explain the results that you observe.

01: #include <stdio.h>

02: int triple_it(int num);

03: int main (int argc, char * argv[]) {
04:     int input = 3, output = 0;
05:     output = triple_it(input);
06:     printf("Value of input after function call = %d\n",input);
07:     printf("Value of output after function call = %d\n",output);
08:     return 0;
09: }

10: int triple_it(int num) {
11:     num = num * 3;
12:     printf("Value of num in function = %d\n",num);
13:     return num;
14: }

 

triple_it(3+2);

triple_it(var1+2);

triple_it(var1++);

triple_it(++var1);

 

18. Which of the following statements are true.

a. A global variable and a local variable cannot have same name.

Ans: False.

 

b. A local variable can be declared anywhere within a function.

Ans: False. Declarations are placed at beginning of a block.

 

c. It is illegal to declare a variable more than once in same scope.

Ans: True. We can use the same name for a variable in nested scope but not in the same scope.

 

19. What will be the output of following program.

#include<stdio.h>

int num = 19;

int silly_function (int num1, int num2) {
    num1 += num2;
    num2 -= 3;
    num =- (num1+num2);
    return (num1+num2);
}

int main (int argc, char * argv[])  {
    int num1 = 5, num2 = 9;
    num2 = silly_function(num1, num);
    printf("%d %d %d\n", num,num1,num2);
}

Ans: -40 5 40

 

 

20. Which of the following statements are true.

a. Local variables are stored in the stack segment.

Ans: True.

 

b. Function specific copy of global data is stored in the stack segment.

Ans: False.