Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
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
Download now
Download
Save 12th-computer-chaptr-13 For Later
Download
Save
Save 12th-computer-chaptr-13 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
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
Download now
Download
Save 12th-computer-chaptr-13 For Later
Carousel Previous
Carousel Next
Save
Save 12th-computer-chaptr-13 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 24
Search
Fullscreen
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: PakistanProgram: 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 20Program: 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; elseky, 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
Functions: Q1. What Is A Function? Also Write Down Its Advantages. Ans
PDF
No ratings yet
Functions: Q1. What Is A Function? Also Write Down Its Advantages. Ans
9 pages
CH 6 Functions
PDF
No ratings yet
CH 6 Functions
29 pages
Unit 3 P4 Functions
PDF
No ratings yet
Unit 3 P4 Functions
32 pages
06 Functions in C
PDF
No ratings yet
06 Functions in C
8 pages
L6- Functions in C.ppt
PDF
No ratings yet
L6- Functions in C.ppt
54 pages
Function Of C
PDF
No ratings yet
Function Of C
28 pages
function in c
PDF
No ratings yet
function in c
22 pages
Function in C Programming
PDF
No ratings yet
Function in C Programming
16 pages
Ch16
PDF
No ratings yet
Ch16
3 pages
Unit 4: Functions and Program Structure 9
PDF
No ratings yet
Unit 4: Functions and Program Structure 9
24 pages
Vignan CP Unit-3
PDF
No ratings yet
Vignan CP Unit-3
32 pages
Functions
PDF
No ratings yet
Functions
48 pages
Function 6
PDF
No ratings yet
Function 6
35 pages
FALLSEM2024-25_BCSE102L_TH_VL2024250109410_2024-08-18_Reference-Material-I (1)
PDF
No ratings yet
FALLSEM2024-25_BCSE102L_TH_VL2024250109410_2024-08-18_Reference-Material-I (1)
36 pages
Week 8
PDF
No ratings yet
Week 8
51 pages
1 Function in C Adv
PDF
No ratings yet
1 Function in C Adv
15 pages
PART 4
PDF
No ratings yet
PART 4
8 pages
Unit-7 (Functions) PDF
PDF
No ratings yet
Unit-7 (Functions) PDF
13 pages
Handouts Module 4 Fns and Struct
PDF
No ratings yet
Handouts Module 4 Fns and Struct
20 pages
Unit 5
PDF
No ratings yet
Unit 5
35 pages
5. Functions and Arrays
PDF
No ratings yet
5. Functions and Arrays
11 pages
Module 3 Notes C Programming
PDF
No ratings yet
Module 3 Notes C Programming
52 pages
New Fakhre Alam Lect 9 (Lab & Theory)
PDF
No ratings yet
New Fakhre Alam Lect 9 (Lab & Theory)
50 pages
C Programming Functions
PDF
No ratings yet
C Programming Functions
18 pages
PPS Chapter-5
PDF
No ratings yet
PPS Chapter-5
22 pages
Python Programming unit - 3
PDF
No ratings yet
Python Programming unit - 3
9 pages
PPS Notes UNIT 3
PDF
No ratings yet
PPS Notes UNIT 3
13 pages
Chapter 1
PDF
No ratings yet
Chapter 1
70 pages
Chapter 5.0
PDF
No ratings yet
Chapter 5.0
101 pages
Lecture 6
PDF
No ratings yet
Lecture 6
50 pages
Functions
PDF
No ratings yet
Functions
122 pages
C Notes
PDF
No ratings yet
C Notes
45 pages
Lectures6 7
PDF
No ratings yet
Lectures6 7
50 pages
MODULE 4-WORKING With Functions
PDF
No ratings yet
MODULE 4-WORKING With Functions
34 pages
PPL Unit 4
PDF
No ratings yet
PPL Unit 4
67 pages
Chap 5.1 2
PDF
No ratings yet
Chap 5.1 2
56 pages
7 - Functions
PDF
No ratings yet
7 - Functions
54 pages
Function
PDF
No ratings yet
Function
63 pages
C Short Notes Module 4
PDF
No ratings yet
C Short Notes Module 4
9 pages
Lab 9 Function Updated
PDF
No ratings yet
Lab 9 Function Updated
9 pages
DSA Session 10
PDF
No ratings yet
DSA Session 10
26 pages
Unit - 4 Notes
PDF
No ratings yet
Unit - 4 Notes
46 pages
FUNCTIONS
PDF
No ratings yet
FUNCTIONS
15 pages
Chapter 6
PDF
No ratings yet
Chapter 6
31 pages
Pop Module 3
PDF
No ratings yet
Pop Module 3
43 pages
Function in C
PDF
No ratings yet
Function in C
46 pages
Unit 4
PDF
No ratings yet
Unit 4
37 pages
Lecture 5
PDF
No ratings yet
Lecture 5
29 pages
Lec 7 Functions
PDF
No ratings yet
Lec 7 Functions
19 pages
Functions in C
PDF
No ratings yet
Functions in C
28 pages
Functions
PDF
No ratings yet
Functions
7 pages
Unit 5
PDF
No ratings yet
Unit 5
37 pages
Session 8
PDF
No ratings yet
Session 8
23 pages
Lecture 8 - Functions
PDF
No ratings yet
Lecture 8 - Functions
38 pages
PPS Ch-9.Functions
PDF
No ratings yet
PPS Ch-9.Functions
40 pages
SPOILER HandOuts Functions and Array
PDF
No ratings yet
SPOILER HandOuts Functions and Array
8 pages
CHAP 11 (Functions)
PDF
No ratings yet
CHAP 11 (Functions)
5 pages
C-Notes Module 3
PDF
No ratings yet
C-Notes Module 3
5 pages
Chapter 10 Functions
PDF
No ratings yet
Chapter 10 Functions
40 pages
Workshop will Advance my career
PDF
No ratings yet
Workshop will Advance my career
1 page
Computer 12 CH09MCQs
PDF
No ratings yet
Computer 12 CH09MCQs
14 pages
Muhammad Ahmed_Life Story
PDF
No ratings yet
Muhammad Ahmed_Life Story
2 pages
ACK Sequence and Series Ppt
PDF
No ratings yet
ACK Sequence and Series Ppt
16 pages
Detailed Akhuwat Presentation for Ahmad - Copy
PDF
No ratings yet
Detailed Akhuwat Presentation for Ahmad - Copy
16 pages