Question

In: Computer Science

Question 3: What is Function? Explain with examples. Provide at least 2 program examples.

Question 3: What is Function? Explain with examples. Provide at least 2 program examples.

Solutions

Expert Solution

What is function?

Function is a group of statements that performs some operations and returns result. If there is a need to run specific lines of code multiple times for different values than insted of writing same statements multiple times for different values you can declare a function that contains the code and then call that function from your main program for different values. So function basically used to provide reusability into the code.

Example 1:

#include <stdio.h>
// function getMax that calculates the maximum of 2 values
int getMax(int num1, int num2) {

   /* local variable declaration */
   int result;
 
    //checks if num1 > num2 then max is num1        
   if (num1 > num2)
      result = num1;
      
    //checks if num1 < num2 then max is num2  
   else
      result = num2;
 
    //return the result     
   return result; 
}
int main() {

   int max;

   //calls getMax method for 20 and 30
   max = getMax(20,30);
   printf("Max is = %d\n", max );
    
   //calls getMax method for 37 and 21
   max = getMax(37,21);
   printf("Max is = %d\n", max );
}

Output:

Example 2:

#include <stdio.h>
#include <stdbool.h>

// function checknum checks if number is even or odd
bool checknum(int num) {

    /* modulo operator returns the remainder of integer division
    if any number is divisible by 2 then it is an even number otherwise
    it is an odd number
    */
    
    //if num is even then return true else returns false
    if(num % 2 == 0){
        return true;
    }
    
    else{
        return false;
    }
}
int main() {

   bool ans;

   //call checknum function for value 20 and store the result in ans variable        
   ans = checknum(20);
   
   //if ans is true then the number is even and prints number is even
   if(ans){
       printf("Number is even \n");
   }
   
   //if ans is false then the number is odd and prints number is odd
   else{
       printf("Number is odd\n");
   }
   
   //call checknum function for value 3 and store the result in ans variable
   ans = checknum(3);
   
   //if ans is true then the number is even and prints number is even
   if(ans){
       printf("Number is even\n");
   }
   
   //if ans is false then the number is odd and prints number is odd
   else{
       printf("Number is odd\n");
   }
   
  
}

Output:


Related Solutions

1. Explain/Discuss what a colligative property is. Provide at least 2 examples of colligative properties. 2....
1. Explain/Discuss what a colligative property is. Provide at least 2 examples of colligative properties. 2. Provide at least one real-life application of freezing point depression. 3. Define the concentration term molality (m). Under what conditions are Molarity and molality approximately the same? 4. Discuss/Explain why the temperature of a substance remains relatively constant during the freezing of liquid to solid.
Simple Answers Please 1. Explain/Discuss what a colligative property is. Provide at least 2 examples of...
Simple Answers Please 1. Explain/Discuss what a colligative property is. Provide at least 2 examples of colligative properties. 2. Provide at least on real-life application of freezing point depression.
1. Define what an "Intergovernmental Revenue" is. Provide at least 2 examples. 2. Assume a local...
1. Define what an "Intergovernmental Revenue" is. Provide at least 2 examples. 2. Assume a local government has determined they will increase taxes in order to raise revenue to support their capital improvements plan. Contrast and compare the merits of increasing property taxes versus sales taxes. Consider the various forces which effect public budgeting in your response.
2) Distinguish between earned and unearned income and provide three examples of each. Explain at least...
2) Distinguish between earned and unearned income and provide three examples of each. Explain at least two differences that result from having earned income instead of unearned.
- Provide at least 2 examples for each of the Internal Control components.
- Provide at least 2 examples for each of the Internal Control components.
Explain each of the following management functions and provide Three (3) examples for each function: planning,...
Explain each of the following management functions and provide Three (3) examples for each function: planning, directing, and controlling. Explain why each management function is important for making business decisions for a company.
(a) Explain at least TWO (2) major aspects of an object-oriented programming (OOP). Provide suitable examples,
(a) Explain at least TWO (2) major aspects of an object-oriented programming (OOP). Provide suitable examples,
1. Explain how IPv4 route decisions are made. Provide at least two examples. 2. Review an...
1. Explain how IPv4 route decisions are made. Provide at least two examples. 2. Review an IPv4 header and discuss the different fields and what each field does. 3. Compare and contrast the major differences between IPv4 and IPv6. Your write-up should include the following details: Header details Datagram size How decisions are made How addresses are assigned
What are common NCDs and health risk behaviors among children? Provide at least 2 examples of...
What are common NCDs and health risk behaviors among children? Provide at least 2 examples of screening and health teaching topics a nurse would focus on targeting children.
Explain what SYSTEMIC RISK is. & give real life examples. provide 2 examples and use at...
Explain what SYSTEMIC RISK is. & give real life examples. provide 2 examples and use at least 4 lines. ECON MONEY AND BANKING CLASS
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT