The Function is called with a string provided inside
the brackets
i.e. hello("Ian Cornelius")
The function returns a greeting welcoming the person
to the module
std::string hello(std::string name) {
return "Hello " + name + ", and welcome to 5062CEM!";
}
#include <iostream>std::string hello(std::string name){return"Hello "+ name +", and welcome to 5062CEM!";}int main(){std::cout <<"hello(\"Ian Cornelius\") -> "<< hello("Ian")<<std::endl;std::cout <<"hello(\"Terry Richards\") -> "<< hello("Terry")<<std::endl;std::cout <<"hello(\"Daniel Goldsmith\") -> "<< hello("Daniel")<<std::endl;return0;}
hello("Ian Cornelius") -> Hello Ian, and welcome to 5062CEM!
hello("Terry Richards") -> Hello Terry, and welcome to 5062CEM!
hello("Daniel Goldsmith") -> Hello Daniel, and welcome to 5062CEM!
Parameters and Arguments (3)
When calling a function, it must be called with the
correct number of arguments
if you have a function with three arguments, then
you have to call the function with three arguments
std::string hello(std::string name, std::string code) {
return "Hello " + name + " and welcome to " + code + "!";
}
#include <iostream>std::string hello(std::string name,std::string code){return"Hello "+ name +" and welcome to "+ code +"!";}int main(){std::cout <<"hello(\"Ian Cornelius\", \"5062CEM\") -> "<< hello("Ian","5062CEM")<<std::endl;std::cout <<"hello(\"Terry Richards\", \"5069CEM\") -> "<< hello("Terry","5069CEM")<<std::endl;std::cout <<"hello(\"Daniel Goldsmith\") -> error: too few arguments to function ‘std::string hello(std::string, std::string)’"<<std::endl;return0;}
hello("Ian Cornelius", "5062CEM") -> Hello Ian and welcome to 5062CEM!
hello("Terry Richards", "5069CEM") -> Hello Terry and welcome to 5069CEM!
hello("Daniel Goldsmith") -> error: too few arguments to function ‘std::string hello(std::string, std::string)’
Parameters and Arguments (4)
Default Values
A function can be called without an argument if a
default value has been assigned to the parameter
the default value will only be evaluated once
std::string hello(std::string name, std::string code="5062CEM") {
return "Hello " + name + " and welcome to " + code + "!";
}
#include <iostream>std::string hello(std::string name,std::string code="5062CEM"){return"Hello "+ name +" and welcome to "+ code +"!";}int main(){std::cout <<"hello(\"Ian\", \"5062CEM\") -> "<< hello("Ian","5062CEM")<<std::endl;std::cout <<"hello(\"Terry\", \"5069CEM\") -> "<< hello("Terry","5069CEM")<<std::endl;std::cout <<"hello(\"Daniel\") -> "<< hello("Daniel")<<std::endl;return0;}
hello("Ian", "5062CEM") -> Hello Ian and welcome to 5062CEM!
hello("Terry", "5069CEM") -> Hello Terry and welcome to 5069CEM!
hello("Daniel") -> Hello Daniel and welcome to 5062CEM!
Goodbye
Goodbye (1)
Questions and
Support
Questions? Post them on the Community
Page on Aula