Question:
need a help in function of C?
Pramod
2011-03-03 20:14:48 UTC
Hi.. I need help in function of C...i know simple programing of function..there a some concept of category functions that are not clear... i have read four categories that are:
1. function with no argument and no return type values
2. function with argument and no return value.
3.function with argument and one return value
4.function with no argument but a return value
5. function that return multiple value.
I want ask that how should i know that which type of function category I have to use and where to use... please explain every category with an example but example must be simple so that i can understand easily. I know it is very lengthy to explain every category with example but friends i really need help in these.. also if you can give me your id..so that if I have any query regarding your answer..i can ask.
thanks in advance
Three answers:
vinodh t
2011-03-04 23:00:01 UTC
hi This is vinodh,Functions are used in programming for doing certain operations many times.for example if u want to add the numbers and multiply by 10 and display the results many times.u can use a function like

void add(int a,int b)

{

int c;

c=a+b;

printf("%d",c*10);

}



so whenever u need to add u can just call the this function.

like

add(4,5);

it will add 4,5 and multiply the reult with 10 and display the results.

like wise add(7,8) will do all the operations listed in the function.thus improving coding efficency.





1)function with no argument and no return value



#include

#include

//function definition

add()

{

int x=6,y=7,z=8;

printf("function with no return value and arguments");

printf("%d %d %d",x,y,z);

}

//main function

void main()

{

int a,b,c;

clrscr();

a=10,b=20;

//function calling

add();



}







for explaining i will consider two sections 1 is main section and another is function section.



add() is the function.when complier tracks this is in main function.the control will transfer to

function definition of add() without any arguments( no value is passed to the function).

whenever u call the add() function it will

int x=6,y=7,z=8;

printf("function with no return value and arguments");

printf("%d %d %d",x,y,z);



and does not return anything to the called function.



u can use this when u need to print certain comments repeatedly or regularly.



2)function with argument and no return value





#include

#include

//function definition

add(int x,int y)

{

int z;

z=x+y;

printf("function with no return value and with arguments");

printf("%d %d %d ",x,y,z);

}

//main function

void main()

{

int a,b,c;

clrscr();

a=10,b=20;

//function calling

add(a,b);



}





here the function is called with values so values 10 and 20 is send to add function

and the value is added and printed.here also no value is returned from the function to the

called function



3)function with argument and with one return value





#include

#include

//function definition

int add(int x,int y)

{

int z;

z=x+y;

printf("function with one return value and arguments");

return z;

}

//main function

void main()

{

int a,b,c;

clrscr();

a=10,b=20;

//function calling

c=add(a,b);

printf("%d",c);

}





in this add() function is called with arguments a=10,b=20 that is assigned to x and y respectively inside

the function and the addition value is returned to the called function.and that is assigned to c

value of c is printed that is 30(10+20)







4)function with no argument and with one return value





#include

#include

//function definition

int add()

{

int z,x=1,y=2;

z=x+y;

printf("function with one return value and no arguments");

return z;

}

//main function

void main()

{

int a,b,c;

clrscr();

a=10,b=20;

//function calling

c=add();

printf("%d",c);

}





in this add() function is called without arguments so x=1 and y=2 is defined inside

the function and the addition value is returned to the called function.and that is assigned to c

value of c is printed that is 3(1+2)



5. function that return multiple value





this is will use the pointers but i don't think so that u will be familiar with this but i will

explain u.

pointer is a variable used to store memory address of other variable.





#include

#include

//function definition

int add(int *x,int *y)

{





printf("function with multiple return value ");



*x=560,

*y=980;

//a and b values will change to 560 and 980

}

//main function

void main()

{

int a,b,c;

clrscr();

a=10,b=20;

//function calling

add(&a,&b);//address of a and b is passed

printf("%d %d",a,b); //560 and 980 wil be printed



}







i am not sure that u will understand this.

give me feedback at vinodhec@gmail.com.

whatever queries u have send me so that it will help both of us.
abaddono1
2011-03-04 04:31:38 UTC
no arguments, no returns: never, you should always return a status so you know if the function executed properly.



function with argument and no return value: never, you should always return a status so you know if the function executed properly.



3.function with argument and one return value: when you need to get a value from a collection of internal values, getIndex(42) or you need to operate on data with data and get a result, getSquareRoot(4).



4.function with no argument but a return value: when you need to retrieve a value from a collection or object. getLastEntry() would be a good example.



5. function that return multiple value. functions may only return 1 value, period, that one value could be a link to many values, but if you want to 'return' a collection of values, you should pass a pointer to some storage to the function. getSquareRoots(&storageArray, doubleArray);
James Bond
2011-03-04 04:36:34 UTC
My Dear,

You know the dogma of programming?.

Did you use any ready made functions such as scanf, printf, sqrt etc.,?

For example. sqrt function takes a number and returns root of it.

Similarly, strlen function takes a string and returns length of it, that is it returns an integer.



If I ask you write a function to find maximum of two integer numbers, we can say it takes two integers and returns one integer. It can be written as

int MAX(int a, int b)

{

return (a
}

Thus, your requirement decides the function characteristics.

For returning more than one value from a function, you can refer www.ritchcenter.com where I have given many approaches.

You can read my books on C, Java


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...