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

12th-computer-chaptr-13

Chp 13

Uploaded by

l236152
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views

12th-computer-chaptr-13

Chp 13

Uploaded by

l236152
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
FUNCTIONS QI. What is a Function? Also write down its advantages. An: © A function is a complete independent program that performs a spec’ task. The main function call these type of functions according to requirement of a program. A program may have repetition of piece of code at various places and it is very difficult to understand and debug larger programs, therefore, a larger program is divided into simple and independent modules called functions ad this type of programming is called modular programming. © Functions make the program easier to understand and maintain, ‘© They increase the readability of the program: © Functions may be reused in multiple programs, ‘© The length of programs can be reduced using functions ‘© Functions can be executed as many times as necessary in the program, When an error arises, rather than examining the whole program, the infected function is debugged only Q2. What are different types of Functions? Explain with the help of examples. Ans. There are two types of functions in C. © Built-in functions © User defined functions, ‘© These are predefined functions provided by high level language. All built-in functions are available in library and therefore called library functions, ‘© Alll the C library functions are defined in header files which must be included in writing C program, © printf{ ) and scanf( ) functions use stdio.h © sqrt() funetion uses math.h © geteh( ), etche( ), elrser( ) functions use conio.h Use The functions created by user are called user defined functions. Q3. How to create and use a user defined function? Ans, int © There are three steps to create a user defined function © Function prototype or function declaration © Funetion definition, © Funetion calling, ion Prototype or Function Declaration:| * It isa statement that provides the basic information that the compiler checks and uses a funetion correctly «The function prototype (declaration) consists of name of function its return type. number of parameters and type of parameters (arguments) Function prototype is usually written at the beginning of the source file (before main function), ¢ Iris similar to variable dec Return_type function_name (parameter list): # Itrepresents the data type returned by the function e.g. int, float # If the function does not return any value then the keyword void is used as return type. It specifies the name of the function e.g. sum, multiply ete. * The information provided to the functions is called parameters or arguments. In function declaration we specify the type of parameters separated by comma. * If no parameter is used the keyword void is used between the parenthesis. fied in the func The parameters speci n header are called formal parameters. The parameters passed to a function in the function call are called actual parameters. Example: © int sum(int, int); or int sum(int x, 0 void display(void); nt y): © float divide(float, int);or float divide(float x, int y); ‘* Scmicolon represents the end of function declaration, Variable declaration and the function logic are implemented in function definition. © The actual code of the function is called function definition *_Itis written outside the main function (before or aft © There are two parts of the function definition © Function header © Function body. © Itis the first line of the function definition. ¢ Ibis similar to function prototype. itis not terminated with semicolon. * Variable declaration and the function lo; are implemented in function definition * _Itis enclosed in curly braces. Return Statement: ‘© The return statement in the function body is used to specify the value retumed by a function, ‘© Itis the last statement in the function body ‘* The general form of return statement is * return value/variable/expression; ‘© When return statement is executed, expression is evaluated and returned as the value of the funetion © Execution of the function body stops when the retum statement is executed. ‘Ifthe type of function is void then there is no need of return statement Function Calli # Itisa process that is used to invoke (exe task. ute) a function to perform a specif * A function can be called at any point in the main program. A function can be called with its name and correct sequence of parameters. ¢ When function call statement is executed. Then the control is transferred to the function body. And the statements in function body are executed. © After executing the last statements in the function, the control is transferred to the calling function Q4. What are local and global variables? Ans. «All variables that we have declared within a pair of curly braces ables and their scope is local. fe called local val © The local variables can only be accessed in the function in which they are declared. # They are not available outside the function # These are also called automatic variables. Variables in the functions are automatically created when the function is executed and destroyed when the function terminates. * The lifetime of a variable is the duration in which a variable exists in memory (the creation and destruction of the memory variable) min which it is accessible. # The scope of a variable refers to the region of a pr # The name of a variable is valid only within its Sy © The variables that we have declared outside all the blocks of the program are called global or external variables. ‘ope not outside the scope. * The scope of a variable refers to the region of a program in which it is accessible. These variables can be accessed in any function at any time during the execution, © They are available for all the functions in the program, following their point of declaration The lifetime ofa global variable is until the t Q.5 What are static variabl Ans. Static Variables: ation of the program, ” Static variables are special variables that are declared inside a function by using the keyword “static”, Like local variables, these can only be accessed in the function in which they are declared but they remain in existence for the lifetime of the program Another difference between local variables and static local variables is that the nitialization in the static variables takes place only once when the function is called for the first time, Each time a function containing a static variable is called, it uses the values n the static variable assigned to it during the last execution of the function. Since a static variable is initialized only once and containing static variables runs faster. The static variables number of times a function was called. Q.6 How is data provided to the function? not destroyed, the function are usually used to count the Ans. Passing Arguments to Functions: The arguments are used to provide data to the function. The function performs operations on the data and produces result. The arguments are placed in parentheses. The arguments are either constants or variables. They are written in the same sequence in which they are defined in the function declaration. The arguments in the function definition of the funetion are called formal arguments or formal parameters. When a function is called, values are provided to the function, These values are called actual arguments or actual parameters. The data type of actual arguments in the function call must match the corresponding data types in the function declaration, Actual arguments ——> — temp (a, b, 16, 2 a, b 16, 2 are actual arguments Formal a guments —-—> void temp (int x, int y, float z) x, y. z are formal arguments Program: Write a program in C language that displays a message by using a function, # include # include void display(void); void main( ) elrser( ); display( ); getch( ); void display(void) printf(“Pakistan™); Output: Pakistan Program: Write a program in C language that find maximum number from three numbers a function, clude # include void max(int, int, int): void main( ) int xyz elrser( ); printf(“Enter first number \t"); inter second number \t"); .&y); printf(“Enter third number \t" scanf(“%d",&2); max(x. y. 2): getch( ); void max(int a, int b, int c) int m; m=a; if(b>m) me=b; if(e>m) 0 printf(“Maximum Number is %d Output: Enter first number 10 Enter second number 20 Enter third number 5 Maximum number is 20 Program: Write a program in C language that find minimum number from three numbers by using a function, # include # include void min(int, int, int); void main( ) int xyz elrser( ); printt( seanft™ printi(“Enter second number \t"); seanft"%d".&y printf(“Enter third number \t" seanft"%d",&z); min(x, y, 2): getch( ); Enter first number \t"); void mintint a, int b, int c) intm; m=a: ifib #include #include float distance(float, float, float, float); void main( ) float x1, x2, yl, y2, d; printf(“\n Enter Co-ordinates of First Point x1 and y1”); scanf(“%f %f”, &x1, &y1); printf(“\n Enter Co-ordinates of Second Point x2 and y2”); scanf(“%of %f", &x2, &y2); d= distance(x1, yl, x2, y2); printf(“\n Distance Between Two Points = %.2f”, d); 1 5 float distance(float x1, float yl, float x2, float y2) float dis, x, y; x=x2-x1; y=y2-yl dis = sqrt(x*x + y*y); return dis; Q6. Write a program hat prompts the user to enter a number and then reverse it through function, Answer: #include int reverse(int); void main( ) line (x +w, y, x tw, y +h); } QS. Write a program that gets two points (x1, yl) and (x2, y2). Displays the distance between them using distance formula in a function. Answer: #include #include float distance(float, float, float, float); void main( ) { float x1, x2, yl, y2, d; printf(“\n Enter Co-ordinates of First Point x1 and y1”); scanf(“%f %f”, &x1, &y1); printf(“\n Enter Co-ordinates of Second Point x2 and y2”); scanf(“%of %f", &x2, &y2); d= distance(x1, yl, x2, y2); printf(“\n Distance Between Two Points = %.2f”, d); 1 5 float distance(float x1, float yl, float x2, float y2) float dis, x, y; x=x2-x1; y=y2-yl dis = sqrt(x*x + y*y); return dis; Q6. Write a program hat prompts the user to enter a number and then reverse it through function, Answer: #include int reverse(int); void main( ) t 5 void draw_astrisk(void) ‘ t intr, ¢; for (c=7;c>=1, c--) 5 t for (r=1sr<=c:r++) ‘ t printh(“*”); 1 5 printf(“\n”); Q8. Write a function isPrime that input a number and return 1 if number is prime otherwise return 0, Answer: #include int isPrime(int); void main() ‘ t int n, res; printf(“\n Enter any Number : “); scanf(“%d", &n); res = isPrime(n); if(res==1) printf(“\n Number is prime and returned value is 1”); else printf(“\n Number is Not prime and returned value is 0”); 1 5 int isPrime(int n) ‘ t int k = while (k<=n-1) 5 t if(n%k==0) return 0; else ky, i return 1; } Q9. Perform arithmetic operations using functions Answer: #include float add(float, float); float sub(float,float); float mul(float,float); float div(float,float); void main ( ) ‘ t float a,b, res; int op; printf(“\n Enter two Numbers : “yy scanf(“%f %f”, &a,&b); printf(“\n Enter | for Addition : “); Enter 2 for Subtraction : printf(“\n Enter 3 for Multiplication : “); printf(“\n Enter 4 for Division : “); printf(“\n Now please press 1 2 3 or 4 : “); scanf(“%d”, &op); switch(op) ‘ t case |: res=add(a,b); break; case 2: res=sub(a,b); break: print case 3: res=mul(a,b); break: res=div(a,b); break: default: printf(“ Choice is invalid “); 1 5 printf(“\n Result = %.2f” res): J float add(float x, float y) s t return x+y; 1 s float sub(float x, float y) ‘ u return x-y; 1 5 float mul(float x,float y) 5 t return x*y; Y J float div(float x, float y) s t if (x==0 || y==0) ‘ v printf(“\n Do Not Use Zero in Division”); return 0; L i if(x>y) return x/y; else return y/x; \ J Q10. Program of Factorial using function: Answer: #include int fact(int); void main() Hf t int n, res; printf(“\n Enter any Number range [1-7] :“); scanf(“%d”, &n): res = fact(n); printf(“\n Factorial of %d is %d”, n, res): j int fact(int n) ‘ t int a=l i: for(i=l; i<=n; i++) a*=i; return a: } Q11. Calculate GCD of two Numbers through function: Answer: #include int ged(int, int); void main() ‘ t int a,b, res; printf(““\n Enter Two Positive Numbers: “); scanf(“%d %d”, &a, &b): res = gcd(a,b); printf(“\n GCD = %d”, res); 5 int ged(int a, int b) t int ged, r, tmp; ber; goto c; } return ged;

You might also like