Sie sind auf Seite 1von 10

PROGRAMMING FUNDAMENTALS

Assignment # 7 Functions in C

Inamullah Taj Reg No: F11A/1446 Dated: 08-01-2012 Submitted to: Sir Shehzad

Q1.1. Define Function.


Ans. A Function is a named block of code that performs some action when it is called by its name in main function.

Q1.2. Why Function is used?


Ans. Functions provide a structured programming approach. A program may use a set of statements repeatedly; in such case Functions are used. Also whole program logic is divided into smaller functions which are easy to maintain and debug.

Q1.3. Name the types of Functions.


Ans. C language provides following types of Functions. User-defined Functions Built-in Functions

Q1.4. Differentiate between function declaration and function definition through an example.
Ans. Function declaration provides information to the compiler about the structure of the function to be used in program. On other hand a set of statements that explains what a function does is called function definition. Example of Function declaration: int myfunction (a, b); syntax= return-type function-name (parameters); Example of Function definition: int myfunction(a, b) {

a=a+b; } syntax= return-type function-name (parameters) { Statement 1; Statement N; }

Q1.5. Function eliminated if

declaration

(prototype)

can

be

Ans. if the function definition is written before main() function.

Q1.6. void func(int a, b, c, float d); Explain?


Ans. void is return-type of function which mean function will not return any value, func is the name of function whereas (int

a, b, c, float d) shows that function has 3 integer & 1 float variable parameters. Q1.7. float func(int a, float b, float & c); Explain?
Ans. float is return-type of function which mean function will return float-type values, func is the name of function whereas

(int a, float b, float & c) shows that function has 1 integer, 2 float variable and address of float variable c as parameters.

Q1.8. Actual arguments may be variables; constants or expression but formal parameters must be variables.
Ans. TRUE

Q1.9. Function can return more than one values using return statement..
Ans. FALSE

Q2.1. Write Function for Factorial.


Ans. #include <stdio.h> #include <conio.h> void factorial(int n); void main() { int num; clrscr(); printf("Enter Number: "); scanf("%d", &num); factorial(num); getch(); } void factorial(int n) { int i; long f=1; for(i=1; i<=n; i++)

f=f*i; printf("\nFactorial = %d ", f); }

Q2.2. Write Function for Fibonacci Series.


Ans. #include <conio.h> #include<stdio.h> void fibonacci(int n); void main() { int r; clrscr(); printf("Enter the number range:"); scanf("%d",&r); fibonacci(r); getch(); } void fibonacci(int n) { int i=0, j=1, k=2, f; printf("Fibonacci series is: %d %d",i, j); while(k<n) { f=i+j; i=j; j=f; printf(" %d",j); k++;

} getch(); }

Q2.3. Integer raise to power another integer.


Ans. #include <conio.h> #include <stdio.h> #include <math.h> void power(int a, int b); void main() { int x ,y; clrscr(); printf("Enter the number: "); scanf("%d",&x); printf("Enter the Raise-to-Power of number: "); scanf("%d",&y); power(x, y); getch(); } void power(int a, int b) { int p; p=pow(a, b); printf(" %d Raise-to-Power of %d = %d", a, b, p); }

Q3. Digit function program. Ans. #include <stdio.h> #include <conio.h> int digit(int num, int k); void main() { int num, k; clrscr(); printf("ENTER THE NUMBER: "); scanf("%d", &num); printf("\nENTER THE VALUE of K: "); scanf("%d", &k); printf("YOUR REQUIRED DIGIT= %d", digit(num,k)); getch(); } int digit (int num ,int k) { int i, rem; for(i=0; i<=k; i++) { rem=num%10; num=num/10; } return rem; }

Q4.1. Items entered.


Ans. #include <stdio.h> #include <conio.h> void item(); void main() { clrscr(); item(); getch(); } void item() { int acounter=0, Acounter=0, ncounter=0, scounter=0; char ch; while(ch!='\r') { printf("\nEnter a number/upper-lower alphabet/symbol: "); ch=getche(); if(ch>=65 && ch<=90) { Acounter=Acounter+1; } else if(ch>=97 && ch<=122) { acounter=acounter+1; }

else if(ch>=48 && ch<=57) { ncounter=ncounter+1; } else { scounter=scounter+1; } } printf("\n\n\t\tYou Entered:"); printf("\n\t%d Symbols", scounter); printf("\n\t%d Numbers", ncounter); printf("\n\t%d Upper-case Letters", Acounter); printf("\n\t%d Lower-case Letters", acounter); }

Q4.2. Integer raise to power another integer.


Ans. #include <conio.h> #include <stdio.h> void cube(long a, long b); void main() { long x ,y; clrscr(); printf("Enter the x-number: "); scanf("%d",&x); printf("Enter the y-number: ");

scanf("%d",&y); cube(x,y); getch(); } void cube(long a, long b) { long i, j, k; i=a*a*a+b*b*b; j=3*a*b; k=i-j; printf("Cube Result= %d", k); }

Q5. Output.
Ans. 1 11 1 7 4

Das könnte Ihnen auch gefallen