Hello there! Expora here again. I'm trying to get the hang of functions now and I made a simple program to calculate percent but I don't seem able to get it to work I can enter the 2 numbers but when i get the results is something like 0041123f. Any ideas? Here's the code:

#include <iostream>

//Percent Calculator

int percent (int x, int y) {
	return (x/y)*100;
}

int main()
{
	int x;
	int y;
	std::cout<<"Entrar dividendo: ";
	std::cin>> x;
	std::cin.ignore();
	std::cout<<"Entrar divisor: ";
	std::cin>> y;
	std::cin.ignore();
	std::cout<<"Su porciento es: "<< percent ;
	std::cin.get();
}

Im thinking something where I defined the funtion? Well any input is apreciated.

Thanks,
Expora

Recommended Answers

All 8 Replies

std::cout<<"Su porciento es: "<< percent ;

should be

std::cout<<"Su porciento es: "<< percent(x,y) ;

Ok gooing to try that now. Thanks!

Ok I tried it but it returns me a zero I'm guessing it is because 87/100 (which was my input) is .87 and int doesnt deal with decimals right I should be using umm float was it?

yes that too .. i forgot.

float percent (float,float);

;) sorry

Works smoothly! Thanks and I think I got the hang fo functions trying switch case now.

Again thanks,
Expora

Good for you.

You guys are great... :)

Think of your entire program as a blueprint. The blueprint describes what and when something is suppose to happen. Functions are a way to extend the functionality(behavior) of your program. You could think of them as the extra pages on a blueprint.

Every function in modern programming languages has this basic format.
[return type] [function name]( [parameters] )
{
[function code]
}

An example function could be:

int add(int a, int b){
return (a + b);
}

"int" is the return type.
"add" is the function name.
"int a, int b" are the parameters, enclosed in parenthesis which represent the start and end of them.
"return (a + b);" is the function code, enclosed in curly braces which represent the start and end of the code.

When you use a function, this is called "calling a function".

-[return type] is the type of information that this function sends back to the calling code. "void" means it doesn't have a return type(and thus doesn't return anything).
-[function name] is the name of the function(obvious, eh?).
-[parameters] are the information that the function requires. Note that the list of parameters must begin with a "(" and end with a ")".
-[function code] is the code that the function performs when called.

The information sent to a function when calling must match the order and types of the parameters, otherwise an error will occur.

Translation/Traducción
____________________________________________________________________________________________

Piense en todo el programa como modelo. El plan describe qué y cuando algo se supone que debe suceder. Las funciones son una manera de extender la funcionalidad (comportamiento) de su programa. Usted podría pensar en ellos como las páginas extra en un plano.

Todas las funciones en lenguajes de programación modernos tiene este formato de base.
[Volver] tipo [cargo] nombre ([parámetros])
(
[Función] código
)

Una función de ejemplo podría ser:

int suma (int a, int b) (
volver (a, b);
)

'Int' es el tipo de retorno.
'Añadir' es el nombre de la función.
'Int a, int b "son los parámetros, entre paréntesis, que representan el comienzo y el final de ellos.
«Retorno (ab)," es el código de la función, entre llaves, que representan el inicio y el final del código.

Cuando se utiliza una función, esto se llama «llamar a una función.

-] [Tipo de retorno es el tipo de información que esta función devuelve al código de llamada. 'Vacío' significa que no tiene un tipo de retorno (y por tanto no devuelve nada).
-] [Nombre de la función es el nombre de la función (obvio, ¿eh?).
- [Parámetros] son la información de que la función requiere. Tenga en cuenta que la lista de parámetros debe comenzar con un '(' y terminar con un ')'.
-] [Código de la función es el código que realiza la función cuando se llama.

La información que se envíe a una función cuando la llamada ha de coincidir con el orden y los tipos de los parámetros, de lo contrario se producirá un error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.