04 Functions

download 04 Functions

of 14

description

It Related

Transcript of 04 Functions

Mathematics, Physics, Chemistry, Biology & Computers (+1,+2)

C-PROGRAMMING (Functions) 4

B.Tech. 1st, 2nd, 3rd & 4th Sem, B.C.A., B.Sc. (Computers & I.T.), PGDCA, B.Pharmacy

Teacher : Sandeep Sangwan (9417565720, 9417524702, 9463741635)

S.C.F. 204, 2nd Floor (Above Chandigarh Sweets Pvt. Ltd.), Sec-14, Panchkula

[ C PROGRAMMING ]Chapter - 04 : Functions

1. Define a function. What is function prototype ?

Ans. A Function is a sequence of some declaration statements and executable statements. A function prototype is the first line of the function definition that tells the program about the type of the value returned by the function and the number and type of arguments. With the help of function prototype, a compiler can carefully compare each use of function with the prototype to determine whether the function is invoked properly i.e., the number and types of arguments are compared and any wrong number or type of the argument is reported.

2. What is function definition ?

OR

What is the syntax or general form of a function?

Ans. In C++, a function must be defined before it is used anywhere in the program. The general form of a function definition is as given below :

return-type function-name (parameter list)

{

body of the function

}

where

return-typespecifies the type of value that the return statement of the function returns. It may be of any valid C data_type. If no value is being returned, it should be void.

The parameter list is a comma-seperated list of variables of a function referred to

as its arguments or parameters. For example : int maximum(int a,int b)

{

if(a>b)

return a;

else

return b;

}

3. What is the statement specifically called that invokes a function ?

Ans. The statement that invokes a function is called function call. A function is called (or invoked) by providing the function name, followed by the paremeters being sent enclosed in parentheses. For example, to invoke a function whose prototype looks like

int sum (int,int);

the function call statement may look like as shown below :

sum (x,y);

where x, y have to be int variables. Whenever a function call statement is encountered, the program control is transferred to the function, the statements in the function-body are executed, and then the control returns to the statement following the function call. For example,

void main()

{

int a=10,b=20;

printf(\nSum of %d and %d is : %d,a,b,sum(a,b)); // function call

}

int sum(int x, int y) //formal parameters

{

return x+y;

}

4. What are actual and formal parameters of a function?

Ans. In functions, there are parameters in the function definition and in the statement that invokes the function i.e. the function call statement. The parameters that appears in function definition are called formal parameters and the parameters that appear in function call statement are called actual parameters. For example :

int main( )

{

int len=15,br=10,area;

area=rectangle(len,br); // len,br are actual parameters

printf(Area of rectangle is : %d,area);

return 0;

}

int rectangle(int x,int y) // x, y are formal parameters.

{

return x*y;

}

5. What is the role of void keyword in declaring functions?

Ans. void data type specifies an empty set of values and it is used as the return type for functions that do not return a value. By declaring a functions return type void, one makes sure that the function cannot be used in an assignment statement.

6. How is call-by-value method of function invoking different from call-by-reference method? Give appropriate examples supporting you answer.Ans. In call-by value method, the value of a variable is passed to the function being called. The called function creates its own work copy for the passed parameters and copies the passed values in it. Any changes that take place remain in the work copy and the original data remains unchanged.

In call-by-reference method, a reference to the original variable is passed. A reference stores a memory location of a variable. The called function receives the reference to the passed parameters and through this reference, it access the original data. Any changes that take place are reflected in the original data. For example, the following program swap values of two integers through both types of function calling mechanism.

int x = 10, y = 20;

void main()

{

printf(Original values before swapping \n x = %d\ty : %d,x,y);

swapCallbyValue(x,y);

printf(\nAfter swapping using call by value \n x = %d\ty : %d,x,y);

swapCallbyRef(&x,&y);

printf(\nAfter swapping using call by reference \n x = %d\ty : %d,x,y);

}

void swapCallbyValue(int a,int b)

{

int temp;

temp=a;

a=b;

b=temp;

printf(\nInside call by value \n x = %d\ty : %d,x,y);

}

void swapCallbyRef(int *a,int *b)

{

int temp;

temp=*a;

*a=*b;

*b=temp;

printf(\nInside call by reference \n x = %d\ty : %d,x,y);

}

The output produced by above program is as given below :

Values initially, x=10, y=20

Values inside swapCallbyValue function, x=20, y=10

Values after swapCallbyValue function, x=10, y=20

Values inside swapCallbyRef function, x=20, y=10

Values after swapCallbyValue function, x=20, y=10

7. What is the role of return statement in a function ? How many values can be returned from a function ?

Ans. A return statement is used to terminate a function whether or not it returns a value. The return statement is useful in two ways. First, an immediate exit from the function is caused as soon as a return statement is encountered and the control passes back to the function call. Second use of return statement is that it is used to return a value to the calling code.

A function may contain several return statements. However, only one of them gets executed because the execution of the function terminates as soon as a return is encountered. Following program code uses multiple return statements :

int maximum(int a,int b)

{

if(a>b)

return a;

else

return b;

}

# Rules about functions :

1) Every C-program contains at least one function.

2) If a program contain only one function it must be main().

3) In case there are more than one function then one of these function must be main().

4) There is no limit on the number of functions that might be present in the C-Program.

5) Functions in the program are called in the sequence specified by the function calls.

6) When all the statements in a called function have been executed control returns to calling function and the statements written after the calling function get executed and finally the program terminates.

7) main() is the function executed first.

8) Any function can be called any number of times.

9) Even the main() function can be called from another function.

10) A function can call itself this feature of function is known as RECURSION

11) A function can be called from another function but cannot be defined in that function.

12) Functions are used to avoid the duplication of lines. Also with functions program can be easily debugged.

Where Variables are Declared

Variable can be declared in three places; inside functions, in the definition of function parameters, and outside of all functions. These positions correspond to local variables, formal parameters, and global variables, respectively.

Local VariablesVariables that can be declared inside a function are called local variables. Local variables can be used only in statements that are inside the block in which the variables are declared. In other words, local variables are not known outside their own code block where a block of code begins with an opening brace and terminates with a closing brace.

A variable declared within one code block has no relationship to another variable with the same name declared within a different code block. The most common code block in which local variables are declared is the function. For example, consider the following two functions:

void func1(void)

{

int x;

x = 10;

}

void func2(void)

{

int x;

x = - 199;

}

The integer variable x is declared twice, once in func1( ) and once in func2( ). The x in func1( ) has no relationship to the x in func2( ). This is because each x is known only to the code within the block in which they are declared.

Global Variables :Global Variables are those variables which are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program execution. We create Global Variables by declaring them outside of any function.

For example, consider the following example

int count ; // count is global variablevoid func1();

void func2();

void main()

{

count = 100;

func1( );

}void func1()

{

int temp;

temp = count;

func2( );

printf (Count is: %d, count); // will print 100 }void func2()

{

int count;

for (count = 1; count < 10; count ++)

printf (.);

}

In this program, the variable count has been declared outside of all functions. Although its declaration occurs before the main( ) function, we could have placed it anywhere before its first use as long as it was not in a function. However, it is usually best to declare global variables at the top of the program.

In the above program, although neither main( ) nor func1( ) has declared the variable count, both may use it. func2( ), however, has declared a local variable called count. When func2() refers to count, it refers to only its local variable, not the global one. If the local and the global variable have the same name, all references to that variable name inside the code block in which the local variable is declared will refer to that local variable and have no effect on the global variable.

Program 1 : WAP to display the sum of two numbers using call by value

#include

#include

int sum(int,int);

void main()

{

clrscr();

int a,b;

printf("Enter two numbers : ");

scanf("%d%d",&a,&b);printf("\nSum of %d and %d is : %d",a,b,sum(a,b)); // a,b are actual arguments

printf("\nSum of 30 and 40 is : %d",sum(30,40)); // 30,40 " " "

getch();

}

int sum(int x,int y) // x,y are formal arguments or formal parameters

{

return x+y;

}

Program 2 : WAP to display the factorial of a number using functions

#include

#include

long int factorial(int);

void main()

{

clrscr();

int n;

printf("Enter number : ");

scanf("%d",&n);

printf("\nFactorial is %d is : %ld",n,factorial(n));

getch();

}

long int factorial(int num)

{

long int fact=1;

for(int i=1;i0)

{

rem=n%10;

sum=sum+(rem*rem*rem);

n=n/10;

}

if(sum==m)

return 't';

else

return 'f';

}Program 6 : WAP to accept a number and display whether it is a prime number or not using functions #include

#include

char prime(int);

void main()

{

clrscr();

int n;

printf("Enter number : ");

scanf("%d",&n);

if(prime(n)=='t')

printf("\n%d is prime",n);

else

printf("\n%d is non-prime",n);

getch();

}

char prime(int n)

{

int i,f=0;

for(i=2;i2)

{

printf("%d\t%d",f0,f1);

fseries(limit-2);

}

else

if(limit==2)

printf("%d\t%d",f0,f1);

else

if(limit==1)

printf("%d",f0);

else

printf("\nSeries not possible");

getch();

}

void fseries(int p)

{

int fib;

static int f0=0,f1=1;

if(p==0)

printf("\nThe series ends here");

else

{

fib=f0+f1;

f0=f1;

f1=fib;

printf("\t%d",fib);

fseries(p-1);

}

}

CREATIVE INFOTECH, SCF-204(ABOVE CHANDIGARH SWEETS PVT. LTD.), 2ND FLOOR, SEC-14, PKL(9417565720, 9417524702,9463741635)

C, C++, DATA STRUCTURES, MATHEMATICS (B.TECH. 1ST, 2ND, 3RD & 4TH SEM, B.C.A., B.SC. COMPUTERS & I.T., PGDCA, MCA, B.PHARMACY)