So, if I have the function foo:
float foo(float a, float b){
return a*b;
}
and I call it from another function, how can I call it like this?
void main(){
foo(scanf("%d"), scanf("%d");
}
scanf doesn't return the input string, I don't want to create a bunch of temp variables. Is that possible, and if so how?
You can't directly return a value from scanf. But you can create a function yourself that returns a value from scanf, but it'll still use temp variables inside of it:
int getInteger() {
int input;
scanf("%d", &input);
return input;
}
Then you can use it like:
foo(getInteger(),getInteger());
However, if you really don't wanna use variables, you can just use get_int() from cs50 library. More info here.
#include <cs50.h> // include cs50 to use get_int
int main(void)
{
foo(get_int("Number 1: "),get_int("Number 2:"));
return 0;
}
Related
a. In the first function print “Hello, I’m in function 1”. Call the second function.
b. In the second function ask the user to enter a float value. Call a third function and pass the value to it.
c. In the third function calculate the square root of the number. Return the square root to the second function, then the first function and then to main.
d. Print the square root in main in TWO decimal places.
Please correct my coding for since I'm not getting the desired output.
float a(float);
void b(float);
void c(float);
void main(float d)
{
printf("In main\n");
printf("back in main \n");
printf("The square root is %f", d);
}
float a(float c)
{
printf("Hello,I'm in function 1");
return (c);
}
void b(float b)
{
float number;
printf("in function 2");
printf("enter a float value");
scanf("%f", &number);
c(number);
return a(number);
}
void c(float a)
{
float power;
printf("in function 3");
power = sqrt(a);
return b(power);
}
What libraries have you included? stdio.h is required for printf function and math.h is also required to implement the sqrt() function.
#include <stdio.h>
#include <math.h>
Your functions are incorrectly defined:
a requires no input and returns no data so it should be void
b returns float variable so it should be labelled as such.
c has type float for input as well as output
void a(void);
float b(void);
float c(float);
printf is not the way to call your functions.
You simply type the name of the function and arguments if you have any into your main function in the order you wish to call them.
example:
void main()
{
//declare your variable
float num;
//calls a function
a();
//calls b function and initializes num as value returned from b
num = b();
//calls c function and passes variable num to parameter in c function
c(num);
}
This should get you headed in the right direction.
I just started with C and am tasked with using a header to house a prototype for a function. The problem is that nothing happens when I'm expecting a prompt for input. I didn't get an error and would like to know where to look at first to solve my problem. This is what I have so far.
LAB2.c
#include <stdio.h>
#include "LAB2HEADER.h"
int main(){
double *p;
double array [10];
p = array;
const int size = 10;
void input(p,size);
return 0;
}
LAB2HEADER.h
#ifndef LAB2HEADER_H_
#define LAB2HEADER_H_
void input (double *array,const int size);
#endif
LAB2HEADER.c
#include <stdio.h>
#include "LAB2HEADER.h"
void input (double *array,const int size){
for (int i = 0; i < size ; i++)
{
printf("Input a value");
scanf("%lf", &array[i]);
}
}
A lot of the notes I look at seem to only either use Int as a parameter or have a function with no needed parameters, could my mistake be in my array pointer is it a problem with the way I made my function?
void input(p,size);
This line makes no sense. If this is supposed to be a function call, you need to remove void.
Also, since your print statement does not end with a newline, nor do you flush stdout before reading in the value, your prompt might still be in the output buffer and not be output until you hit the newline AFTER entering the value.
I have just written a sample program to understand the working of functions in C. I declared a function in C and call it during my programs execution. However my compiler gives me a warning saying unused function. My code looks like this :
#include <stdlib.h>
#include <stdio.h>
int test_function(x);
int main(){
int x;
char letter[] ={"HAAA"};
char cmpre[] = {"AHD"};
int value;
for(int i=0; i<4;i++)
{
if(letter[i] == cmpre[i])
{
x=0;
}
}
int test_function(x)
{
if (x==0)
{
printf("the letters are the same");
}
return value;
}
printf("To check if the letters are the same go to the function");
test_function(x);
return 0;
}
The program seems to execute fine but I get a warning in the fourth line where I declared the function in the start of the program. The warning is :
Multiple markers at this line
- parameter names (without types) in function declaration [enabled by
default]
- Unused declaration of function 'test_function'
I think the way I am calling my function is not right. Could somebody please help me. Thnak you in advance.
Disclaimer: nested functions are non-standard C and I only know (of) the GNU extension for this. As such anything I claim here may well be untrue in another implementation. My recommendation is that you just don't use them at all.
Your nested test_function is shadowing the global declaration. So the test_function you declared above main is never called, because the call inside main refers to the nested function. Hence, you get a warning.
You should declare int test_function outside of main
for example.
int test_function(int x)
and then call the function in main.
value = test_function(x)
This is what your code should look like:
#include <stdlib.h>
#include <stdio.h>
int test_function(x)
{
int value = 0;
if (x==0)
{
printf("the letters are the same");
}
return value;
}
int main(){
int x = 0;
char letter[] ={"HAAA"};
char cmpre[] = {"AHD"};
int value = 0; // unused
for(int i=0; i<4;i++)
{
if(letter[i] == cmpre[i])
{
x=0;
}
}
printf("To check if the letters are the same go to the function");
test_function(x);
return 0;
}
Note that if you dont need a return value you could make the function void.
And initialize your variables. You may search hours to find such a error
I have a simple function that I am trying to create. I need this function to add 6 to whatever number the user decides to enter into the program. I have been working with this code for an hour now and cannot figure out what I'm doing wrong, even after looking at multiple examples from the course I am taking. I really appreciate the help.
Here is my code:
#include <stdio.h>
void closing(void);
void addSix(void);
int x;
int result;
int main()
{
int x;
printf("Please enter a number to add to 6: ");
scanf("%d", &x);
getchar();
addSix();
closing();
closing();
return 0;
}
void closing(void)
{
printf("That's all folks.\n");
}
void addSix(void)
{
int result = x+6;
printf("Result: %d\n", result);
}
You've got both a global x variable outside of main(), and a local x inside of main(). The code inside of main() writes to the local x while the code in addSix() reads from the global x.
Remove the int x declaration in main() so that both places access the global x.
You have x declared as both a global variable and variable local to main. When you pass x by pointer to scanf, it refers to the local variable, not the global.
You can remove the local declaration of int x in main but this isn't really the best solution. Generally, global variables should be avoided when possible (though they are, of course, sometimes necessary/the best tool for the job).
The best solution in this case is to make x a parameter to addSix(). There are a few options here:
You can have addSix return the sum and then use that return value
You can pass the address of x to addSix and have the function modify x itself by using a pointer.
The former would look like this:
int addSix(int x) {
return x + 6;
}
The latter would look like this:
void addSix(int * x) {
*x += 6;
}
You have two variables named x. One is a local to main, and the other is a global variable.
The function addSix cannot see the local in main. It can only see the global variable.
You should change addSix so that it is passed the value as a parameter.
void addSix(int x)
{
printf("Result: %d\n", x+6);
}
Call the function like this:
addSix(x);
Or perhaps you want your function to return a value:
int addSix(int x)
{
return x+6;
}
Which you can call like this:
int result = addSix(x);
Both of your global variables are needless. Remove them.
You have a global x, and a local x variable in which you actually save the user input.
Using only the global variable will do the trick for you
#include <stdio.h>
int x;
void addSix(void)
{
int result = x+6;
printf("Result: %d\n", result);
}
int main()
{
printf("Please enter a number to add to 6: ");
scanf("%d", &x);
getchar();
addSix();
return 0;
}
I'm learning C and doing the exercise of the function chapter. So i have written a small programm with 3 files and two small functions. Sincerly it does not work. I have no errors, the functions are simply not executed and i don't know why.
First of all this is my headerfile which only declares my functions.
//employee.h
int addEmployee(void);
int printEmployee(int i);
So the next file is for the definition of the functions.
//employee.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int numE;
int i;
int addEmployee(void)
{
printf("Please type in the number of your employees: ");
scanf_s("%d", &numE);
i = numE;
return i;
}
int printEmployee(int i)
{
printf("%d", i);
getchar();
getchar();
return i;
}
And the last file is used to execute the functions.
//lab6.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int main ()
{
int addEmployee();
int printEmployee();
return 0;
}
Using
int main ()
{
int addEmployee();
int printEmployee();
return 0;
}
you're basically declaring 2 new functions with 0 arguments.
You're not calling your "old" functions.
This should work, as others have pointed out:
int main ()
{
int emp = addEmployee();
printEmployee(emp);
return 0;
}
Because you're calling addEmployee(), storing it's result to emp and then printing emp using printEmployee. Since printEmployee is declared with one parameter, you just put emp in and it will work.
When you call a function you do not put the return type in front of the call. The call is simply the name of the function and any parameters you are calling it with. So your main function should look like this:
int main() {
addEmployee();
printEmployee(1);
return 0;
}
EDIT: So in your employee.c file, you are trying to use addEmployee() to take a number of employees from the command line and store it in the variable i right? And you want printEmployee() to tell you how many employees were entered? Here's how you would do that.
//employee.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int i;
int addEmployee(void)
{
int numE;
printf("Please type in the number of your employees: ");
scanf_s("%d", &numE);
i = numE;
}
int printEmployee()
{
printf("%d", i);
getchar();
getchar();
}
Here's what I did.
First, I made numE a variable local to the addEmployee function that uses it. Generally you should keep variable scope as small as possible. That means keep them down to the lowest level they are used. In this case, numE is only needed by addEmployee() so that's its scope.
Second, I removed the parameter from int printEmployee(int i). It was overriding your i variable at the file level. So you were storing the number read into numE in i but then when you entered printEmployee() you were creating a new, empty i that hid it. When you called printEmployee(1) from main, you were passing the value 1 into i in printEmployee(int i). By removing the parameter, you stop hiding employee.c's i.
Finally, I removed the returns. A function doesn't have to return anything in C. And if you are not going to use the return, then it's just an extra line of code to include it.
There's one more change you'll have to make to make this work, in your lab6.c file. Remove the parameter from the call to printEmployee()
//lab6.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int main ()
{
addEmployee();
printEmployee();
return 0;
}
Should work the way you expect it now.
you want:
int main ()
{
addEmployee();
printEmployee(1);
return 0;
}
Change this:
int main ()
{
int addEmployee();
int printEmployee();
return 0;
}
To this:
int main ()
{
addEmployee();
printEmployee();
return 0;
}
You're re-declaring the functions instead of calling them.
You'll also have to change your printEmployee function to not accept an integer argument; it seems like it should just be using the same global variable as addEmployee. This is a bad idea though; global variables are generally to be avoided. addEmployee should probably return the employee ID, which you could store and then pass into printEmployee.