Is that possible to send two values to function and return both separately without using data structures such as arrays?
like this:
#include<stdio.h>
int f(int a,int b)
{
a*=2;
b*=2;
return ?????????
}
int main()
{
int x=5,y=10,k;
k=f(x,y) ?????????
printf("%d",k); ????????
}
You cannot directly return more than one item (where an item could be a structure containing multiple items within). However you can "pass by reference" if you're comfortable with pointers.
#include <stdio.h>
void f(int *a, int *b)
{
*a *= 2;
*b *= 2;
}
int main()
{
int x=5, y=10;
f(&x, &y);
printf("new x: %d, new y: %d", x, y);
}
See the results of this at http://ideone.com/p4Xiqv
No, its not possible to return more than one values without using any data structures. But, you can pass any number of arguments.
Related
I've written some code in regard to a uni assignment, but I keep stumbling across the problem where my custom function doesn't give any other output then zero.
Basically, I'm asking how to retrieve the result of the function to use in my code.
Here I will paste my code.
#include <stdio.h>
//math.h is included via the header file because the compiler liked it better.
int V;
int H;
int R;
int result;
#include "A1_header.h"
int main()
{
//small introduction
printf("Welcome to the volume test game\n");
printf("We will start with spheres. \n");
printf("Please input your sphere radius here:");
scanf("%d", &R);
CalcSphVolume(&V, &R);
printf("please input your result:");
scanf("%d", &result);
if(result == V){
printf("Congratulations, your answer is correct!\n");
}
else{
printf("Wrong answer, the correct result was %d \n", V);
}
return 0;
}
Below is my .h file which I use to define functions.
#ifndef Point
#define Point
#include <math.h>
//these are the functions for Sphere calculations
void CalcSphVolume(int V, int R) {
V = 1.33*3.14(R*R*R);
}
void CalcSphRadius(int V, int R) {
R = cbrt(V/4.1762);
return;
}
//these are the functions for the Cone calculations
void CalcConVolume(int V, int R, int H) {
V = 0.33*(3.14*(R*R))H;
return;
}
void CalcConHeight(int V, int R, int H) {
H = V/(0.33*(3.14*(R*R)));
}
void CalcConRadius(int V, int R, int H) {
R = sqrt(V/(0.33*3.14H));
}
//these are the functions for the Cilinder calculations
void CalcCilVolume(int V, int R, int H) {
V = 3.14*H*(R*R);
}
void CalcCilHeight(int V, int R, int H) {
H = V/(3.14(R*R));
}
void CalcCilRadius(int V, int R, int H) {
R = sqrt(V/(3.14*H));
}
#endif
I am not sure how you are running the code, because under ordinary circumstances this code will not compile.
There are a couple of things that are wrong in this code -
void CalcSphVolume(int V, int R) {
V = 1.333.14(RRR);
}
Firstly, I think there might be an error in your formatting, because RRR is not the correct way to compute, what you want is (R*R*R), similarly, 1.333.14 is not an accepted datatype. From context, since it is the volume of a sphere, this should be
void CalcSphVolume(int V, int R) {
V = 1.333 * 3.14 * (R*R*R);
}
Now that this is out of the way, there are some issues with this function (and similar to the other functions in your code.)
You are mixing types here - 1.333 is double, but V is int, so your actual answer will be implicitly converted to an integer, and you will lose precision. So V should be of the type double. So you get a more accurate answer.
void CalcSphVolume(double V, int R) {
V = 1.333 * 3.14 * (R*R*R);
}
Another thing to note here is that your functions parameters are by value and do not accept pointers. This means that any result that you get in this void function, will be lost unless you explicitly return it. Local variables only exist in the scope of the function. Since you are passing a pointer to the function and trying to populate the value outside the function, you should modify the function and function signature to
void CalcSphVolume(double* V, int* R) {
int r = *R // for clarity
*V = 1.333 * 3.14 * (r*r*r);
}
We have to dereference to get the "actual" value that is held by the pointer. You cannot apply arithmetic logic in this manner to raw pointers.
An alternative way to accomplish the same is
double CalcSphVolume(int R) {
double V = 1.333 * 3.14 * (R*R*R);
return V; // or simply return 1.333 * 3.14 * (R*R*R)
}
Here you are passing by value, but returning the value computer back to the caller. So you could use this function like so -
double Volume = CalcSphVolume(R);
This is much more clearer in this case, instead of having to pass pointers all over the place.
For your use case, using pointers is not necessary - consider using pointers when you have to mutate or pass large objects (Imagine a really huge array, so you don't want to create a copy it each time you use it in a function) which cannot be declared on the stack.
The issue is your function parameters are taking in an integer rather than a pointer to the memory address.
What you want to do instead is:
void CalcSphRadius(int *V, int *R)
{
*R = cbrt((*V)/4.1762);
return;
}
Now, the function is taking in a pointer to V and R and will read/write to their memory address.
As shown above, don't forget to dereference your pointers by using an asterisk '*' so that way you are writing to the value stored in those addresses, rather than just doing pointer arithmetic.
You can also retrieve values by using return types for functions.
int CalcSphRadius(int V)
{
return cbrt(V/4.1762);
}
And then use them to assign variables like so:
R = CalcSphRadius(V);
Your .h file name should be A1_header.h
Also, the code can be like this:
#ifndef Point
#define Point
#include <math.h>
#include <stdio.h>
//these are the functions for Sphere calculations
void CalcSphVolume(int V, int R);
void CalcSphRadius(int V, int R);
//these are the functions for the Cone calculations
void CalcConVolume(int V, int R, int H);
void CalcConHeight(int V, int R, int H);
void CalcConRadius(int V, int R, int H);
//these are the functions for the Cilinder calculations
void CalcCilVolume(int V, int R, int H);
void CalcCilHeight(int V, int R, int H);
void CalcCilRadius(int V, int R, int H);
#endif
#include "A1_header.h" in the wrong place.
It should be like
//directives
#include <stdio.h>
#include "A1_header.h"
int main(void)
{
variable declaration;
statements
}
Please try to learn the structure of the C program.
You need to declare the variables inside the main function.
When you call the function CalcSphVolume(V, R); why the address of the variable? It should be:
printf("Please input your sphere radius here:");
scanf("%d", &R);
CalcSphVolume(V, R);
Arrange all the functions properly
eg: CalcCilHeight function should be properly formatted with *
void CalcCilHeight(int V, int R, int H)
{
H = V/(3.14 * (R * R));
}
#include<stdio.h>
int areaOfRectangle(int,int);
int perimeter(int,int);
int main()
{
int l,b;
scanf(" %d %d",&l,&b);
printf("%d %d %d %d",l,b,areaOfRectangle(l,b),perimeter(l,b));
return 0;
}
int areaOfRectangle(int a,int b)
{
int area;
area=a*b;
return area;
}
int perimeter(int c,int d)
{
int meter;
meter=2(c+d);
return meter;
}
why this error:called object is not a function or function pointer at line: meter=2(c+d)?
Also, can I use the same variable a,b to pass in perimeter function?
Your code meter=2(c+d);should be changes as meter=2*(c+d);
You can use the same variable a,b to pass in perimeter function, A parameter is just a local variable.
In this statement:
int perimeter(int c,int d)
{
int meter;
meter=2(c+d);
return meter;
}
You'll obviously get a syntax error since the compiler won't detect 2(c+d) as you're trying to implicitly multiply 2 with (c + d). Rather, if you explicitly define 2 * (c + d) then it'll no longer show you any error.
One side tip, you don't need to use any local variables inside a function if you just want to return a simple return statement, rather you may use:
return 2 * (c + d);
The declaration is redundant thence.
int main ()
{
int a, b;
call(&b);
printf("%d, %d",a , b);
}
void call(int *ptr)
{
}
Desired output:
50, 100
How to write the call function so as to modify both the variables to get the desired output??
Not sure where the values 50 and 100 are coming from or exactly what you are asking but maybe this will help with your question.
Since C is pass by value you need to send pointers to actually change the value inside another function.
Since the call function will have pointer values you need to dereference the pointers before changing the value.
Here is an example:
void call(int *a, int *b)
{
*a = 50;
*b = 100;
}
int main()
{
int a, b;
call(&a, &b);
printf("%d, %d\n", a, b);
}
While we are exploring the many ways this output could be achieved, consider that the function could store state in a static variable:
#include <stdio.h>
void call(int *ptr);
int main(void)
{
int a, b;
call(&a);
call(&b);
printf("%d, %d\n",a , b);
}
void call(int *ptr)
{
static int store = 0;
store += 50;
*ptr = store;
}
Program output:
50, 100
Note that you may also be able to do this as follows, without any modifications to main(). But be warned that this method invokes undefined behavior! It is undefined behavior to write to a location past the end of an array object, and in the case of a and b, these are considered to be array objects of size 1. Here we are assuming that this write will work, and that a and b are stored next to each other in memory. We further assume that a has the higher address in memory.
I would say that you should never do this, but I can see no other way to modify a from the function call() without knowing the address of a. You have been warned.
void call(int *ptr)
{
*ptr = 100;
*(ptr + 1) = 50;
}
Try something like this:
void call(int *ptr)
{
*ptr = 100;
}
int main ()
{
int a, b;
a = 50;
call(&b);
printf("%d, %d",a , b);
}
See demo
Maybe you want this:
int main ()
{
int a, b;
call(&a, &b);
printf("%d, %d",a , b);
}
void call(int *ptr1, int *ptr2)
{
*a = 50;
*b = 100;
}
To change a local variable in function a by calling function b you have two options.
1) Let function b return a value that you assign to the variable in function a. Like:
int b() {return 42;}
void a()
{
int x = b();
printf("%d\n", x);
}
This does, however, not seem to be what you are looking for.
2) Pass a pointer to the variable to function b and change the variable through that pointer
void b(int* p) // Notice the * which means the function takes a pointer
// to integer as argument
{
*p = 42; // Notice the * which means that 42 is assigned to the variable
// that p points to
}
void a()
{
int x;
b(&x); // Notice the & which means "address of x" and thereby
// becomes a pointer to the integer x
printf("%d\n", x);
}
int main()
{
int a,b;
call(&b);
printf("%d, %d\n", a,b);
}
int call(int *ptr)
{
int *m;
m = ptr++;
*ptr = 50;
*m = 100;
}
A few lessons ago I learned about variables, and got a question in my homework about swapping two numbers - I used a third variable to solve this question.
The solution looked somewhat like this:
#include <stdio.h>
int main(void) {
int x, y;
scanf("%d %d", &x, &y);
// swappring the values
int temp = x;
x = y;
y = temp;
printf("X is now %d and Y is now %d", x, y);
}
Now I'm learning about functions, and I wanted to try and solve the previous question with a helper swap function.
This is the code I've written:
#include <stdio.h>
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main(void) {
int a = 3, b = 4;
swap(a, b);
printf("%d %d\n", a, b);
}
I don't know why, but the output is still 3 4 even though I changed the value inside the swap() function.
Why is this happening?
Pass address of x and y as arguments to function. Right now they are local variables, changes are not made to original variables .
Do as follows-
void swap(int *x,int *y){
/* dereference pointers and swap */
int temp = *x;
*x = *y;
*y = temp;
}
And call in main like this -
swap(&x,&y);
What you are doing is passing parameter by value. It means that during the function call, copies of parameters are created. So inside the function you are working on copies of actual variables.
Instead you need to pass it as a reference. Please read more about pass-by-value vs pass-by-reference.
#include <stdio.h>
void swap(int& x,int& y) //Instead of passing by value just pass by reference
{
int temp=x;
x=y;
t=yemp;
}
int main() {
int a=3,b=4;
swap(a,b);
printf("%d %d\n",a,b);
return 0;
}
EDIT:
C does not have references. Above code will work in c++ instead. To make in work in C, just use pointers and de-reference it inside the function.
I have a problem with this simple program because it doesnt give me the true outcome. I just want to sum two arguments in the first function and then use the outcome in the second one. It will be nice to have overall outcome in the main function. Also I would like to ask the same question with arrays.
#include <stdio.h>
#include <stdlib.h>
int sum()
{
int a=2;
int b=3;
int s=a+b;
printf("sum=%d\n",s);
return s;
}
int sum2(int s)
{
int c=5;
int d=c+s;
}
int main(int s,int d)
{
sum();
printf("sum=%d\n",s);
printf("sum2=%d\n",d);
getchar();
return 0;
}
There are many problems with this code:
int main(int s, int d) won't do what you think. Command-line arguments to your program come in string format. So you would need to use int main(int argc, char *argv[]).
The variables s and d in main() are completely independent to the variables in sum() and sum2(). So changing their values in those functions will not affect the original variables.
You're not even calling the second function!
You can do things like this:
int sum(int a, int b)
{
return a+b;
}
int sum2(int c)
{
return c+5;
}
int main(void)
{
int x = 2;
int y = 3;
int z = sum(x,y);
int w = sum2(z);
printf("z = %d\n", z);
printf("w = %d\n", w);
}
First of all, s is a local variable inside sum( ). Hence it cannot be available outside the function.
int sum() {
// ..
int s = a+b; // local variable, hence local scope
// ..
}
Also, secondly, int main(int s,int d) won't work since in command line arguments come in String format. So can't use a int there
I won't tell you the answer (lol but others have) but I'll give you these clues to figuring it out.
Ask your self which functions are returning data and which ones aren't.
Clue: the function needs a return to return some data.
Then ask yourself which functions' returns are actually being used.
Clue: to collect the data returned from a function you need to assign the result to a variable like so
int i;
i = somefunct();
You can't access the value of variable 's' outside the function sum() since it is out of scope. You'll have to return the value to your main() function. Also your main function parameters are incorrect. You need something more like this:
#include <stdio.h>
#include <stdlib.h>
int sum(int a, int b)
{
int s=a+b;
printf("sum=%d\n",s);
return s;
}
int sum2(int c, int sum)
{
return c+sum;
}
int main(int argc, char *argv[])
{
int val1 = sum(2, 3);
printf("sum=%d\n",val1);
int val2 = sum2(5, val1);
printf("sum2=%d\n", val2);
getchar();
return 0;
}