int multiply(int a, char *b)
{
return a b;
}
The code does not execute properly. Try to figure out why.
c program language
Thank your kindhearted help !
the question has been solved simply by me, thank everyone!
int multiply(int a, int b)
{
return a*b;
}
There's more than one error in there.
First, you can't return two variables in the same function, you should return a or the content of the pointer that the variable b in pointing to.
So, you could either use:
return a;
to return the variable a.
or you could use
return *b;
to return the content of the adress that b is pointing to.
If you want to multiply, as the name of the function, you should use:
return a*(*b)
Your function need to return an integer
int multiply(int a, char *b)
but you try to return an int (a) and a char* (b)
if you want to return b (and only b) use
char *multiply(int a, char *b)
You must create a variable that the result will be the multiplication of the parameters and then return the value like the created variable, as I showed.
int multiply (int a, int b) {
int result;
result = a * b;
return(result);
}
Related
#include <stdio.h>
#define SPACE ' '
void branching_if_judgement(int a, int b){
if (a > b){
printf("a(%d) is larger than b(%d)\n", a, b);
}else {[![enter image description here][1]][1]
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
}
}
char branching_if_judgement_char(int a, int b){
char res;
if (a > b){
printf("a(%d) is larger than b(%d)\n", a, b);
res = 'Y';
}else {
printf("a(%d) is smaller or equal to b(%d)\n", a, b);
res = 'N';
}
return res;
}
int main() {
branching_if_judgement_char(2,3);
}
I follow the example to run the code. And, there are missing the main function in the slide. I add it
So, my question is how to combine all function in the one output, just like the slide.
:
The function signature tells us that
char branching_if_judgement_char(int a, int b)
it returns a thing of type char, and it takes two things of type int.
So when you call it, you're passing the two things to it, but not taking the thing it's returning.
branching_if_judgement_char(2,3);
Correct code should be
char p = branching_if_judgement_char(2,3);
printf("%c\n", p);
Please find some time to read
https://www.tutorialspoint.com/cprogramming/c_functions.htm
https://www.tutorialspoint.com/cprogramming/c_data_types.htm
#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.
In C, I have a function that calculates the product of a and b. If a < b it returns false otherwise returns true. How can I return the product when the function is defined as boolean?
bool multiplication(int a, int b)
{
int c = a*b;
if (a < b)
return false;
else
return true;
}
How to return bool and double simultaneously?
A function can only have one return type. But you can use a workaround of using a struct containing a bool and an int members as stated in one of the comments.
You can achieve your goal of writing a multiplication function of positive integers with a recursion logic the following way too:
int multiplication(int a, int b) {
if(b <= 1) return a;
return a + multiplication(a, --b);
}
There are a few ways that come to mind that you can use to return a boolean and double simultaneously:
Return the boolean as the return value and return the double as an output parameter.
Return a structure that has the boolean value as one field and the double value as another field.
Return the boolean value as the return value and return the double value using a global variable.
Here's what code for these solutions respectively might look like:
bool multiplication(int a, int b, double* product)
{
*product = a * b;
return a >= b;
}
And:
struct retval {
bool b;
double p;
}
struct retval multiplication(int a, int b)
{
struct retval result;
result.b = a >= b;
result.p = a * b;
return result;
}
And lastly:
double product;
bool multiplication(int a, int b)
{
product = a * b;
return a >= b;
}
Of these, the second solution doesn't fit your question exactly as it no longer returns a bool (but instead returns a struct which can have other downsides in C). The third solution, I despise because I strongly dislike the use of globals (globals make unit testing harder, keeps the using function from being able to be a pure function, causes threading issues).
So this leaves the first solution.
Hope this answers your question.
You can define a new parameter as output.
define a struct included bool and int data.
Initiate the struct, then return it.
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.
After compiling my C code with -Wall activated, the following warnings appeared
left operand of comma operator has no effect
which are related with the the multiple arguments presented in my return statements. The story is the following:
Assume to have a bunch of dynamically allocated 3D arrays (A,B and C) and want to do some manipulation on them. The arrays are defined as pointer to pointer to pointer and allocated using malloc (the standard procedure). The manipulation of them will occur in seperate functions. For some reason I declare the function as a triple pointer and as follow:
***func( double ***A, double ***B, double ***C)
{
do some work here on A, B and C
return(A, B, C);
}
I know that the arrays are passing into the function as reference so essentially there is no need of returning something from this function. But, can you tell me why someone would declare a function this way. This staff confuses me. Thanks in advance
return(A, B, C) is not C, you can use a struct to return more than one arguments.
struct array3d{
double* A;
double* B;
double* C;
};
struct array3d* func(struct array3d* p) {
/* do some work here on p->A, p->B and p->C */
return p;
}
Here is an working example with *** pointers:
#include <stdio.h>
#include <malloc.h>
struct array3d {
double*** A;
double*** B;
double*** C;
};
struct array3d* func(struct array3d* p) {
/* do some work here on A, B and C */
***p->A /= 42.0;
***p->B /= 42.0;
***p->C /= 42.0;
return p;
}
int main()
{
struct array3d arr;
struct array3d* p_arr;
double A[] = { 1.0, 3.0}; // ...
double B[] = {-1.0, -2.0};
double C[] = { 2.0, 4.0};
double* p1A = A;
double* p1B = B;
double* p1C = C;
double** p2A = &p1A;
double** p2B = &p1B;
double** p2C = &p1C;
arr.A = &p2A;
arr.B = &p2B;
arr.C = &p2C;
p_arr = func(&arr);
printf("(A = %f, B = %f, C = %f)\n", ***p_arr->A, ***p_arr->B, ***p_arr->C);
return 0;
}
The code
***func( double ***A, double ***B, double ***C)
{
do some work here on A, B and C
return(A, B, C);
}
is simply wrong, even though it compiles:
If the function returns everything via its arguments A, B, and C, it should not have a return type. I. e., it should be declared to return void:
void func( double ***A, double ***B, double ***C) {
The syntax return(A, B, C) does not do what you think it does. It does not construct a list, or pass three values to return. Instead, it evaluates the expression A, throws its value away, evaluates B, throws its value away, evaluates C, takes the value of C as the value of the expression (A, B, C), and finally returns that value from the function. (Google "C comma operator" for more information.)
If your function is declared as returning void (as it probably should be), there is simply no need for a return statement.