0% found this document useful (0 votes)
19 views

10th Comp Ch 5 LongQ

10th computer

Uploaded by

rajajamilafzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views

10th Comp Ch 5 LongQ

10th computer

Uploaded by

rajajamilafzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
Q. What is problem solving? Describe the divide and conquer approach to solve a problem? Problem solving is a process of identifying a problem and finding the best solution for it. Different solutions of a problem can be identified and the best solution is selected, Divide and Conquer Divide and conquer is a good problem solving approach. It divides a problem into multiple smaller parts or sub-problems. It becomes easier to focus on a single small problem instead of the whole big problem. The whole problem is solved by solving the smaller problems ‘one by one. C language provides functions that allow the user to solve a programming problem using divide and conquer approach. Q. What is a function? Explain different types of functions in C. Function A function is a block of statements that performs a particular task. ;program can have many functions. Each function in the program must have a unique name. Every program has a main function that performs the tasks programmed by the user. Other functions can also be written and used multiple times. ‘Types of Functions C language provides the following types of functions 1. Built-in Funetions A type of function that is available in C Standard Library is called built-in funetion or library function, These functions are predefined in C language. Built-in functions make programming faster and easier. C language provides many built-in functions, These functions are used to perform mathematical calculations, string operations and input/output operations ete. These functions are defined in different header files. Some examples of built-in functions are printf), seanf(), getchO, sqrt and pow(). 2. User-defined Functions A type of function that is defined by the programmer is called user-defined function. Each user-defined function has a unique name. A program may contain many user-defined functions. These functions are written according to the exact need of the user. Q. Briefly explain the benefits of u ing functions. are as follows: Some important benefits or advantages of using function 1, Reusability The code in a function is written once but can be reused many times. It allows the user to execute the same statements multiple times without writing it again, Suppose a function Line displays a line on the screen, It can be executed in different parts of the program to display the line again and again. 2. Separation of Tasks ‘The functions allow the user to separate the code of one task from the code of other tasks, It becomes easier to find the errors and make changes in small funetions instead of the whole big program. 3. Handling the Complexity of Problem The use of functions reduces the complexity of the problem. Functions divide the program into smaller units. It is easier to maintain the functions than one big program. Any change in a funetion does not affect other parts of the program. 4. Readability The use of functions improves the readability of the program, Each funetion contains the code to solve a specific task. It becomes easier to find the required piece of code from a specific function, Q. What is function signature? Explain with examples. Function Signature Funetion signature is used to define the inputs and outputs of a function, The inputs of a function are called parameters. The output of a function is called return value. The function signature ends with a semicolon. It is also known as function declaration or function prototype. Syntax The general structure of function signature is as follows: return-type function-name (typel, type2, ... ,typeN)s return-type It indicates the type of value that will be returned by the function, The keyword void is used if fiction returns no value. function-name It indicates the name of the function. Each function should have a unique name. A function is called by its name. typal, type2,typeN These are the parameters passed to function as input. They are written in parentheses. If there are many parameters, these are separated by commas, If there is no parameter, empty parentheses are used or keyword void is written in parentheses. Examples Some examples of function signature are as follows: Example 1 void show(void); The above example shows that the function show takes no parameters and returns no value. Example 2 —_ void line(char); ‘The above example shows that the function Hine takes one character as parameter and returns no value. Example 3 int cube(int); The above example shows that the function eub and retums integer value. Example 4 int max(int, int, int); The above example shows that the function max takes three integers as parameters and returns an integer value. Example 5 float avg(float, float, float); The above example shows that the function avg takes three float values as parameters and returns a float value, takes one integer as parameter Q. Briefly describe the process of defining a function in C language. The function definition describes how the function performs the task assigned to it, The function definition consists of two parts known as function header and function body, Syntax ‘The basic syntax of defining a function is as follows: return-type function-name (typel , type2, ... , typeN) t Funetion-body; 3 1. Funetion Header ‘The first line of a function definition is known as function header. It indicates the return type, fiznetion name and arguments. The function header is same as function signature. ‘The only difference is that function header does not end with a semicolon 2. Function Body ‘The set of statements which are executed in the function to perform the specified task is known as function body. The statements are written in curly braces { }. The parameters given to a function can also be used in the function body Example 1 void show) t printf("Hello world!"); 7 ‘The above example defines a function show. The return type of the function is void that ‘means it does not return any value. It does not take any parameters. It displays a simple message on the screen when it is called. Example 2 int add(int a, int by t int e: emath; return ©; The above example defines a fimetion add. The return type of the function is int that ‘means it returns an integer value, It takes two integer values as parameters. It adds these values and returns the result. Q. What is a function call? Explain an example. Funetion Call ‘The statement that activates a function is own as function call, A function is called with its name, When a funetion is called, the following steps take place: 1. The control moves to the function that is called. 2. All statements in the function body are executed, 3. The control returns back to the calling function, When the control returns back to the calling function, the remaining statements in the calling function are executed. Syntax ‘The syntax of function call is as follows: function-name (yall , val2, ... , valN); function-name —_It indicates the name of the function to be called. vall vai2, vaIN These aré the arguments that are passed to the funetion as input. They are written in the parentheses. If there are many arguments, these are separated by commas. The empty parentheses are used if there is no argument. Example include show(), printf("C Programming"); Output: Welcome main) Programming Bye printf("Welcome \n"); show(); printf("Bye"): 3 Explanation ‘The above program works as follows: 1. The control moves to the main() function when the program starts. 2. The first statement in main() function is executed that displays "Welcome, 3. The second statement calls the function show() and the control moves to the function. 4, The statement in show( function is executed that displays "C Programmin, 5. The control moves back to main( function. 6. The last statement is executed that displays "Bye" and the program ends. Q. What are different arrangements of functions in C program? Give examples. The functions in C program can be arranged in two different ways as follows: 1. Function Definition before Calling Function A function can be defined before the calling function. The function signature is not required in this ease, Example #inelude void show) { printf("Welcome to C Language"); 3 void main() { show(): 3 The above example defines a function show before the main() function, The function signature is not required as the funetion definition appears before the calling.fuunction. 2. Function Definition after Calling Function A function can be defined after the calling function, The function signature is required in this case Example include void show; main() { show(): 3 void show() t printf(“Welcome to © Language"): $ The above example defines a function show after the mainQ function. The function signature is required as function definition appears after the calling function. Q. Explain the process of passing values to the function with an example, ‘The values that are passed to a function are called arguments. The arguments are written in the parentheses. If there are many arguments, these are separated by commas. The empty parentheses are used if there is no argument, ‘The variables in the funeti. ‘The values of arguments are copied to parameters when a function is called. The function does not make any change to the values of the arguments. Example inelude sum(int a, int b) mn definition that receive the values are called parameters. int s: s=atb; printf("Sum 3 void main) Output: { int x=5, y= 10: Sum = 15 sum(x, y): } Explanation The above program works as follows: 1. The control moves to the main() function when the program starts. 2. The first statement declares and initializes the variables x and y. 3. The second statement calls the function” sum(x; y) with the-variables-x and y as arguments, 4, The control moves to the function sum and the values of x and y are copied to the parameters a and b correspondingly. 5. The function sum adds both variables and stores the result in s. ‘The value of s is finally displayed on the screen. 7. The control moves back to main( function and the program ends. Q. Is it necessary to the pass the arguments with the same name as parameters? Explain with example, It is not necessary to pa: the same or different names for arguments and parameters. However, the variables used in the the arguments with the same name as parameters. The user can use function are still the copy of the original variables even if the same names are used. Example #include ‘void square(int n) t n=n*2, printf(""Value of n in square() = %d \n", n); 3 void main() Output: f Value of n in square() = 10 int n= 5; Value of n in main() = 5 square(n): printf("Value ofn in main() = %d \n", n), Q. How does a function return value? Explain with an example. A function can retum a single value. The return type indicates the type of value returned by the function. For example, int is used as return type if the function returns an integer value. The keyword void is used as return type if the function returns no value. The keyword return is used to return the value back to the ealling function, The control moves back to the calling function along with the returned value when the return statement is executed in a function. Syntax ‘The syntax for retuming a value is as follows: return expressi expression: Itcan be a variable, constant or an arithmetic expression whose value is returned to the calling function, Example #inelude int multiply(int x, int y) t int result; result= x" y; return result, 3 void main() { Output: int a=5,b=10, ¢; 5x 10-50 = multiply(a, b); printi("%d x %d = %d", a, b, ©) 3 Explanation ‘The above program works as follows 1. The control moves to the mainO) function when the program starts. 2. The first statement declares the variables a, b, ¢ and initializes the variables a and b. 3. The second statement calls the function multiply(a, b) with the variables a and b as, arguments. 4, The control moves to the function multiply and the values of a and b are copied to the parameters x and y correspondingly. 5. The function multiply multiplies both variables and stores the result in result 6. The value of result is finally retuned back to the main function. 7. The control moves back to main function and the returned value is stored in the variable ¢, (WEBSITE: ill. FREEILM.COM] [NOTES: 10TH COMPUTER - UNIT 5 - LONG Q] 8. The last statement displays the value of e and the program ends. Program 5.1 Write a program that inputs two numbers in main function and passes the numbers to a function. The function returns the maximum number back to the main function where the returned number is displayed. #include int max(int a, int b); void main() t int x, y. m; Output: printf(""Enter two numbers: Enter two numbers: 30 10 scanfi"%d %d", &x, &y); Maximum number is 30 m=max(xy); printf("Maximum number is %d" a, int b) ifa > b) return a; else return by Program 5.2 Write a function isPrime that takes a number as input and returns 1 if the input number is prime, otherwise returns 0, Use this function in mainO: #include int isPrime(int n) t im i++) iffn%i-=0) return 0; return 1; main) Output: int num; Enter a number: 97 printf("Enter a number: "), 97 isa prime number. scanf{"%6d", &num), iffisPrime) printf("%d is a prime number.", num); else printf("ad is not prime number.", num); Program 5.3 Write a function that takes a positive number as input and returns the sum of numbers from 0 to that number. return s, > void main() { Output: int num, s; Enter a number: 97 printf("Enter a number: "); 97 is a prime number. seanf{"%d", Senum); if{num >= 0) t s= sum(num); print{("Sum of numbers from 0 to %d is %d.", num, s); 3 else printf("Sorry, you entered a negative number."); } at: freeilm786@)gmail.com visit: freeilm.com

You might also like