Can u return two values using return keyword? If yes, how?
If no, why?
Answers were Sorted based on User's Feedback
Answer / c.saranya
no. because return keyword return only one value.that ia 0
or 1.
| Is This Answer Correct ? | 15 Yes | 4 No |
Answer / pancuz
yes we can return two or more values from a function using
return keyword...
Use structure...return type of the function will b 'struct'
and v ll store value in struct type.
| Is This Answer Correct ? | 10 Yes | 2 No |
Answer / abhijit roy
in c a function can only written one value
the return value could be of any data type
| Is This Answer Correct ? | 10 Yes | 4 No |
Answer / vignesh1988i
sorryt sorry i made a mistake... i
in the line q=q/2 is wrong
correct as *q=*q/2;
and *w=*w/2;
since addresses cant be divided...
very sorry
| Is This Answer Correct ? | 9 Yes | 6 No |
Answer / vikas shakya
Using the return statement u can only return one value at a
time.
So you can either return the value of a variable like you
can return an integer, or you can return pointer (which may
contain more than one values), which is pointing to
dynamically allocated location, Like in given below example:
//Returning two values from a function.
#include "stdio.h"
#include "malloc.h"
int *values()
{
int *ptr;
ptr = (int*)malloc(2);
*ptr = 10;
*(ptr+1) = 20;
return ptr;
}
int main()
{
int *ptr = values();
printf("%d\n%d",*ptr,*(ptr+1));
return 0;
}
| Is This Answer Correct ? | 2 Yes | 3 No |
Answer / vignesh1988i
ya we can return two or more than two values..... it's
possible..
by using concept of POINTERS..... but no need of return
keyword at all.....
instead of call by value in the function use call by
reference concept....
take the following program:
int fun(int *,int *);
void main()
{
int j=800,k=1000;
fun(&j,&k);
printf("%d",j,k);
getch();
}
int fun(int *q,int *w)
{
q=q/2;
w=w/2;
}
the output of the followiung is : 400 & 500.
how it's possible, i ll explain,
since we are calling by reference we
are sending the address of the two variables. so in fun.
definition we are catching it by pointers..... so that
pointer variable is holding the address of the two variables
in main fun. which is passed through address.... so in the
function we are changing the values of j & k.... so this
will change the value directly in the address of those two
variables j & k....... so implicitely two values are
returned wit out return keyword....
| Is This Answer Correct ? | 7 Yes | 9 No |
What does s c mean in text?
Heyyy All, Just a challenge . A C program with if Else if(){ /// insert sumthing print ("in if") // insert sumting } else { ///// insert sumthing print ("in else"); //// insert sumthing } can anyone modify it so that program prints. if and else both
What is the difference between i++ and i+1 ?(in terms of memory)
#include <stdio.h> int main() { if ("X" <"x") printf("X smaller than x "); } my question is whats the mistake in this program? find it and please tell me..
Write a program to swap two numbers without using a temporary variable?
what is the difference between #include<stdio.h> and #include"stdio.h" ?
Write a c program to find, no of occurance of a given word in a file. The word is case sensitive.
One of the Institutes contains 5 student groups. Every group contains 4 students. Institute wants to store student group’s details in array. Group should contain group member’s details (name and registration number and age), project name, and mark of the group.
How does C++ help with the tradeoff of safety vs. usability?
what is the different bitween abap and abap-hr?
How to find the given no is odd or even without checking of any condition and loops. (Hint: Using array)
What is the output of the below program and how it is? void main() { static int var=5; printf("%d",var--); if(var) main(); }
8 Answers MindFire, TCS, Tech Mahindra,